在 Flutter 中将参数传递给云函数

Passing Parameters to Cloud Functions in Flutter

在 Flutter 中使用 HttpsCallable 插件时,我正在努力获取要传递到我的 Cloud Function 的参数。

我的 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}`);
    // })
});

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

我的 Dart 文件:

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),
    );
  }
}

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

您的客户端代码正在尝试调用 callable function, but your function is different type of HTTP function。它们不兼容 - 请务必阅读文档以了解它们的不同之处。

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

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

尝试req.body

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

代替查询

用法

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',
});