【问题标题】:Flutter Firebase Cloud functions: The data couldn’t be read because it isn’t in the correct formatFlutter Firebase Cloud 功能:无法读取数据,因为它的格式不正确
【发布时间】:2020-10-16 01:35:21
【问题描述】:

我尝试使用 Flutter 项目中的 cloud_functions 插件调用我的云函数,代码如下:

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

dynamic resp = await callable.call(<String, dynamic>{'id': id, 'chatId': chat.chatId});

并得到以下错误:

ERROR: PlatformException(3840, The data couldn’t be read because it isn’t in the correct format., null)

通过我的研究,我发现当您忘记将区域放在服务器端和客户端时会出现问题,但错误仍然存​​在。

我也尝试通过成功的http请求:

var parameters = {'id': id, 'chatId': chat.chatId};
var url = "https://europe-west3-{MY_DOMAIN}.cloudfunctions.net/helloWorld";
await http.post(url, body: parameters).then((res) {...}

所以我认为问题出在插件上,我可能忘记了一些东西。有什么想法吗?

云功能(测试):

exports.helloWorld = functions
  .region('europe-west3')
  .https.onRequest((request, response) => {
    try {
      response.send('Hello from Firebase!');
    } catch (e) {
      console.log(e);
      throw new functions.https.HttpsError('calc-error', e);
    }
  });

【问题讨论】:

  • 我发现这个question 与你的相似,我认为这个可以帮助你。正如您所提到的,当您不指定区域时会出现问题,所以如果您的云功能位于您用来调用它的同一区域(欧洲西部3),您能否分享一下?
  • @AndieVanille 我更新了问题,您可以看到我已经将区域放入函数中。
  • 我在 Callable 函数中遇到了同样的问题

标签: firebase flutter google-cloud-functions


【解决方案1】:

当您在 Flutter 应用上使用 callable 时,请尝试将您的函数转换为使用 onCall 而不是 onRequest

使用onCall的Firebase函数:

export const helloWorld = functions.https.onCall((data, context) => {
  functions.logger.info("data:", data);
  return {
    message: "bye!"
  };
});

颤振应用:

模拟器设置:

  // after: Firebase.initializeApp()
  FirebaseFunctions.instance.useFunctionsEmulator(origin: 'http://localhost:5001');

调用函数:

  FirebaseFunctions functions = FirebaseFunctions.instance;

  HttpsCallable callable = functions
      .httpsCallable('helloWorld');
  final results = await callable({
    'name': 'Erick M. Sprengel',
    'email': 'erick@mail.com'
  });

注意onCallonRequest 之间的区别。 很容易转换,但我认为最好检查这个问题:Firebase Cloud Functions: Difference between onRequest and onCall

额外提示:

  • 我用的是模拟器,没有设置区域。
  • 请记住在每次更改后重新构建您的函数。我正在使用npm run build -- --watch

【讨论】:

    【解决方案2】:

    就我而言,我有错误的可调用函数名称

    FirebaseFunctions.httpsCallable('wrong name in here')
    

    【讨论】:

    • 坐了 3 个小时,你对我的问题给出了正确的答案。愚蠢的错字.. 投票赞成。
    猜你喜欢
    • 2021-10-05
    • 2016-10-15
    • 1970-01-01
    • 2019-07-28
    • 2018-04-06
    • 2020-07-26
    • 2021-11-11
    • 2021-12-04
    相关资源
    最近更新 更多