Search code examples
firebaseflutterdartgoogle-cloud-functionscallable

Flutter - Firebase cloud function callable functions wrong format


I'am trying to modify my firestore database with a callable function from cloud function... Here is my cloud function:

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",
      };
    });

And here is my call of this cloud function on my flutter app:

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

But I don't know why, every time I try to call my function, I get this error and nothing is called :

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>

Do you have any idea why ?


Solution

  • Ok, I found out that the problem was attached to the region... I forgot to put the .instanceFor(region: 'europe-west1')

    So I had to do :

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

    Instead of :

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