Flutter 中向云函数传递参数

Raf*_*sas 0 firebase flutter google-cloud-functions flutter-web

在 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}`);
    // })
});
Run Code Online (Sandbox Code Playgroud)

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

我的飞镖文件

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),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Dou*_*son 5

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

如果您想使用客户端代码来调用函数,则需要遵循可调用函数的说明,并使用onCall来声明它,而不是onRequest.


小智 5

用法

import 'package:cloud_functions/cloud_functions.dart';
Run Code Online (Sandbox Code Playgroud)

获取可调用函数的实例:

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
    functionName: 'YOUR_CALLABLE_FUNCTION_NAME',
);
Run Code Online (Sandbox Code Playgroud)

调用函数:

dynamic resp = await callable.call();
Run Code Online (Sandbox Code Playgroud)

调用带参数的函数:

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