【问题标题】:Passing Parameters to Cloud Functions in Flutter在 Flutter 中向云函数传递参数
【发布时间】:2021-01-24 02:44:50
【问题描述】:

在 Flutter 中使用 HttpsCallable 插件时,我正在努力获取参数以传递给我的云函数。

我的 Cloud Functions 中的日志显示未传递任何参数。

我的云函数index.js

// acts as an endpoint getter for Python Password Generator FastAPI
exports.getPassword = functions.https.onRequest((req, res) => {
    const useSymbols = req.query.useSymbols;
    const pwLength = req.query.pwdLength;
    // BUILD URL STRING WITH PARAMS
    const ROOT_URL = `http://34.72.115.208/password?pwd_length=${pwLength}&use_symbols=${useSymbols}`;
    const debug = {
        pwLenType: typeof pwLength,
        pwLen: pwLength,
        useSymbolsType: typeof useSymbols,
        useSymbols: useSymbols,
    };
    console.log(req.query);
    console.log(debug);
    // let password: any; // password to be received
    cors(req, res, () => {
        http.get(ROOT_URL).then((response) => {
            console.log(response.getBody());
            return res.status(200).send(response.getBody());
        })
            .catch((err) => {
            console.log(err);
            return res.status(400).send(err);
        });
        // password = https.get(ROOT_URL);
        // response.status(200).send(`password: ${password}`);
    }); // .catch((err: any) => {
    //   response.status(500).send(`error: ${err}`);
    // })
});

我确保不仅要记录我正在寻找的单个参数,还要记录整个查询。

我的飞镖文件

Future<String> getPassword() async {
  final HttpsCallable callable = new CloudFunctions()
      .getHttpsCallable(functionName: 'getPassword')
        ..timeout = const Duration(seconds: 30);

  try {
    final HttpsCallableResult result = await callable.call(
      <String, dynamic>{
        "pwLength": 10,
        "useSymbols": true,
      },
    );

    print(result.data['password']);
    return result.data['password'];
  } on CloudFunctionsException catch (e) {
    print('caught firebase functions exception');
    print('Code: ${e.code}\nmessage: ${e.message}\ndetails: ${e.details}');

    return '${e.details}';
  } catch (e) {
    print('caught generic exception');
    print(e);
    return 'caught generic exception\n$e';
  }
}

class DisplayPassword extends StatelessWidget {
  final String pw = (_MyPasswordGenPageState().password == null)
      ? 'error'
      : _MyPasswordGenPageState().password;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(pw),
    );
  }
}

我想传入请求的密码长度和一个布尔值来标记是否需要符号。生成的密码应显示在警报小部件中。

【问题讨论】:

    标签: firebase flutter google-cloud-functions flutter-web


    【解决方案1】:

    用法

    import 'package:cloud_functions/cloud_functions.dart';
    

    获取可调用函数的实例:

    final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
        functionName: 'YOUR_CALLABLE_FUNCTION_NAME',
    );
    

    调用函数:

    dynamic resp = await callable.call();
    

    带参数调用函数:

    dynamic resp = await callable.call(<String, dynamic>{
      'YOUR_PARAMETER_NAME': 'YOUR_PARAMETER_VALUE',
    });
    

    【讨论】:

      【解决方案2】:

      试试 req.body

      const useSymbols = req.body.useSymbols;
      const pwLength = req.body.pwdLength;
      

      而不是查询

      【讨论】:

        【解决方案3】:

        我认为您最好在本地处理设备上的密码检查。用户在创建密码时会立即收到,来自云函数的 https 调用可能会延迟,并且可能会导致难看的加载“我们正在检查您的密码”

        【讨论】:

        • 我认为您对我要创建的内容略有误解。我用更好的细节更新了这个问题,因为我仍在恢复 null。可以找到Here。我不希望直接调用我的 Fast-API,因为我希望我的网络和移动应用程序的服务相同。
        【解决方案4】:

        您的客户端代码正在尝试调用callable function,但您的函数是不同类型的HTTP function。它们不兼容 - 请务必阅读文档以了解它们的不同之处。

        如果你想使用你的客户端代码来调用一个函数,你需要按照callable functions的说明,并使用onCall来声明它,而不是onRequest

        【讨论】:

          猜你喜欢
          • 2021-10-17
          • 2020-10-01
          • 2022-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-25
          相关资源
          最近更新 更多