首页 > 解决方案 > Flutter - Firebase 云函数可调用函数格式错误

问题描述

我正在尝试使用云函数中的可调用函数修改我的 firestore 数据库......这是我的云函数:

exports.addUserDisplayName = functions.region("europe-west1")
    .https.onCall((data, context) => {
      functions.logger.log("User displayname updated",
          data.userUid, data.displayName);
      admin.firestore().collection("users").doc(data.userUid).update({
        displayName: data.displayName,
      });
      return {
        status: "done",
      };
    });

这是我在颤振应用程序上调用此云功能的方法:

HttpsCallable addUserDisplayName = FirebaseFunctions.instance.httpsCallable("addUserDisplayName");
      await addUserDisplayName.call({
        'userUid': userCredential!.user!.uid,
        'displayName': "$firstName $lastName",
      },);

但我不知道为什么,每次我尝试调用我的函数时,都会收到此错误并且没有调用任何内容:

flutter: [firebase_functions/3840] The data couldn’t be read because it isn’t in the correct format.

#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
<asynchronous suspension>
#2      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:22:24)
<asynchronous suspension>
#3      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:34:37)
<asynchronous suspension>
#4      AuthProvider.createUser (package:kcount/providers/auth_provider.dart:50:7)
<asynchronous suspension>

你知道为什么吗?

标签: firebaseflutterdartgoogle-cloud-functionscallable

解决方案


好的,我发现问题与区域有关...我忘了把.instanceFor(region: 'europe-west1')

所以我不得不这样做:

HttpsCallable addUserDisplayName = FirebaseFunctions.instanceFor(region: 'europe-west1').httpsCallable("addUserDisplayName");
      await addUserDisplayName.call({
        'userUid': userCredential!.user!.uid,
        'displayName': "$firstName $lastName"
      },);

代替 :

HttpsCallable addUserDisplayName = FirebaseFunctions.instance.httpsCallable("addUserDisplayName");
      await addUserDisplayName.call({
        'userUid': userCredential!.user!.uid,
        'displayName': "$firstName $lastName",
      },);

推荐阅读