Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 无法调用Flatter Firebase Cloud函数_Flutter_Google Cloud Functions - Fatal编程技术网

Flutter 无法调用Flatter Firebase Cloud函数

Flutter 无法调用Flatter Firebase Cloud函数,flutter,google-cloud-functions,Flutter,Google Cloud Functions,当我试图从Flatter调用可调用函数时,在使用Firebase云函数时出错 flutter: caught generic exception flutter: PlatformException(functionsError, Firebase function failed with exception., {message: NOT FOUND, code: NOT_FOUND}) 下面是我如何使用cloud_函数调用cloud函数:^0.4.2+3 import 'package:c

当我试图从Flatter调用可调用函数时,在使用Firebase云函数时出错

flutter: caught generic exception
flutter: PlatformException(functionsError, Firebase function failed with exception., {message: NOT FOUND, code: NOT_FOUND})
下面是我如何使用cloud_函数调用cloud函数:^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);
        }
      }
import'包:cloud_函数/cloud_函数.dart';
_检查(字符串id)异步{
httpscalable callable=CloudFunctions.instance
.getHttpScalable(functionName:'checkUserFavorites');
试一试{
最终HttpScalableResult结果=wait callable.call(
{
“id”:id,
},
);
打印(结果、数据);
}关于CloudFunctionsException捕获(e){
打印(“捕获的firebase函数异常”);
打印(电子代码);
打印(电子信息);
打印(如详细信息);
}捕获(e){
打印(“捕获的一般异常”);
印刷品(e);
}
}

根据,当前运行云功能的区域支持云功能,但并非所有区域都支持云功能

根据您在评论中分享的内容,我想说您的问题有三个方面:

  • europe-west1
    :该函数可能已过时,因为您收到一个未经检查的数据格式错误,这表明它需要与默认函数不同的数据/格式

  • europe-west2
    :此功能未部署在此区域,错误消息
    消息:未找到
    中暗示了这一点

  • 默认函数(未知区域):这是函数的最新版本,位于不同于
    europe-west1
    europe-west2
    的区域,并接受调用 使用您正在发送的格式的数据

注意:您可以在云功能仪表板上检查当前部署了哪些区域的云功能,如下图所示:

此外,我怀疑您使用的默认区域是
us-central1
,因为根据文档:

默认情况下,函数在
us-central1
区域中运行


为了解决您的问题,我建议您将当前版本的函数重新部署到您打算使用的欧洲西部地区。

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

部署Firebase云功能时,您可以选择任何首选区域(离应用程序越近越好)。比如说

// using DigitalOcean spaces
exports.generateCloudImageUrl = functions
    .region('europe-west3')
    .https.onCall((reqData, context) => {
...
}
当您想从Flatter应用程序调用此函数时,必须指定区域,否则全部转到默认的
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! 
有关init,请参见

更新,从,您可以如下初始化

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

这个问题似乎与云功能本身有关。当它们在region('europe-west1')或region('europe-west2')中运行时,会出现PlatformException(3840,数据无法读取,因为它的格式不正确,null)或PlatformException(functionsError,Firebase函数因异常而失败。,{消息:未找到,代码:未找到})。不是所有区域都支持可调用函数吗?当不定义区域时,函数按预期运行,没有任何问题或错误。如果我不配置默认值,它实际上将是us-central1。我的数据库位于europe-west3。我的所有其他功能都部署到了europe-west1,并且它们按预期工作。但是,在europe-west1中部署可调用函数不起作用。我现在使用了默认的us-central1,它可以工作。我想知道europe-west1是否支持可调用函数。正如我在回答中所评论的那样,
europe-west1
被列为支持云函数的区域,所有这些区域都支持所有类型的函数,包括可调用函数,所以我不会说这是该区域本身的问题。您在该部署中是否有任何错误或日志,以便我们进行故障排除?部署到europe-west1时,调用函数时我甚至看不到日志。请尝试完全删除该区域中的函数实例并重新部署它。实际上,这不是区域问题,我只是通过一个简单的解决方案找到了原因,我将在下面的答案中添加代码示例。在我添加到2021年3月的/web/index.htmlas之前,所有这些都无法解决我的问题:因此您可以初始化为
final FirebaseFunctions\u FirebaseFunctions=FirebaseFunctions.instanceFor(地区:'europe-west3')
thnx@goldenseju,刚刚通过您的评论更新了答案。