Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firebase Cloud functions: The data couldn’t be read because it isn’t in the correct format

I tried to call my cloud function using the cloud_functions plugin from my Flutter project with the following code:

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

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

And get the following error:

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

By my research, I saw that the problem can appear when you forget to put the region on the server and client side, but the error persist.

Also I try to pass by http request who succeed:

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) {...}

So I think the problem come from the plugin where I maybe may have forgotten something. Any ideas ?

Cloud function (test):

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

like image 727
Lab Avatar asked Nov 17 '25 02:11

Lab


1 Answers

As you are using a callable on your Flutter app, try to convert your function to use onCall instead of onRequest:

Firebase function using onCall:

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

Flutter app:

emulator setup:

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

calling the function:

  FirebaseFunctions functions = FirebaseFunctions.instance;

  HttpsCallable callable = functions
      .httpsCallable('helloWorld');
  final results = await callable.call({
    'name': 'Erick M. Sprengel',
    'email': '[email protected]'
  });

Be careful about the difference between onCall vs onRequest. It's easy to convert, but I think it's better to check this question: Firebase Cloud Functions: Difference between onRequest and onCall

Extra tips:

  • I'm using the emulator, and I'm not setting the region.
  • Remember to re-build your function after each change. I'm using npm run build -- --watch
like image 165
Erick M. Sprengel Avatar answered Nov 18 '25 21:11

Erick M. Sprengel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!