Flutter Firebase Cloud 功能无法调用 [英] Flutter Firebase Cloud function can not be called

查看:20
本文介绍了Flutter Firebase Cloud 功能无法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting an error whilst using Firebase Cloud Functions when I try to call a callable function from Flutter.

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

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

Here is how I try to call the cloud function with using 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);
        }
      }

推荐答案

我也遇到过类似的问题,经过几天的调试和实验,在研究了Flutter 的云函数插件.

I have experienced similar issues, and with few days of debugging and experimenting I found the solution only after studying the source code of Cloud Functions Plugin for Flutter.

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

When you deploy Firebase Cloud function, you can choose any region of preference (closer to your application the better). For example

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

当你想从 Flutter 应用调用这个函数时,你必须指定区域,否则全部转到默认的 us-central1.参见示例代码,了解如何使用部署在特定区域的函数

When you want to call this function from Flutter app, you must specify the region, otherwise all goes to us-central1 which is default. See example code on how to use a function deployed in a specific region

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! 

请参阅 cloud_function 源 用于初始化.

see cloud_function source for init.

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

Update, as of recent release, you can initialize as below;

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

这篇关于Flutter Firebase Cloud 功能无法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆