Flutter Firebase Cloud 函数无法调用

Ben*_*Ben 4 flutter google-cloud-functions

当我尝试从 Flutter 调用可调用函数时,在使用 Firebase Cloud Functions 时遇到错误。

flutter: caught generic exception
flutter: PlatformException(functionsError, Firebase function failed with exception., {message: NOT FOUND, code: NOT_FOUND})
Run Code Online (Sandbox Code Playgroud)

这是我尝试使用 cloud_functions 调用云函数的方法:^0.4.2+3

import 'package:cloud_functions/cloud_functions.dart';
      _check(String id) async {
        HttpsCallable callable = CloudFunctions.instance
            .getHttpsCallable(functionName: 'checkUserFavorites');
        try {
          final HttpsCallableResult result = await callable.call(
            <String, dynamic>{
              'id': id,
            },
          );
          print(result.data);
        } on CloudFunctionsException catch (e) {
          print('caught firebase functions exception');
          print(e.code);
          print(e.message);
          print(e.details);
        } catch (e) {
          print('caught generic exception');
          print(e);
        }
      }
Run Code Online (Sandbox Code Playgroud)

gul*_*yuz 9

我也遇到过类似的问题,经过几天的调试和实验,我在研究了Cloud Functions Plugin for Flutter的源代码后才找到了解决方案。

部署 Firebase Cloud 函数时,您可以选择任何偏好区域(越靠近您的应用程序越好)。例如

// using DigitalOcean spaces
exports.generateCloudImageUrl = functions
    .region('europe-west3')
    .https.onCall((reqData, context) => {
...
}
Run Code Online (Sandbox Code Playgroud)

当你想从 Flutter 应用程序调用这个函数时,你必须指定区域,否则一切都以us-central1默认的方式进行。请参阅有关如何使用部署在特定区域的函数的示例代码

final HttpsCallable generateCloudImageUrl = new CloudFunctions(region: "europe-west3")
      .getHttpsCallable(functionName: 'generateCloudImageUrl');

// NB! if you initialize with 'CloudFunctions.instance' then this uses 'us-central1' as default region! 
Run Code Online (Sandbox Code Playgroud)

请参阅init 的cloud_function 源

更新,从最近的版本开始,你可以初始化如下;

FirebaseFunctions.instanceFor(region: "europe-west3").httpsCallable(
            "generateCloudImageUrl",
            options:
                HttpsCallableOptions(timeout: const Duration(seconds: 30)));
Run Code Online (Sandbox Code Playgroud)