flutterfirebasegoogle-cloud-functions

Calling Firebase Cloud Functions from Flutter gives 'The data couldn’t be read because it isn’t in the correct format.'


I have these firebase functions

import { onRequest } from "firebase-functions/v2/https";

export const helloWorld = onRequest((request, response) => {
  console.log("hello"); // <--- I just added it
  response.send("Hello from Firebase!");
});

export const helloWorld2 = onRequest((request, response) => {
  console.log("hello 2"); // <--- I just added it
  response.send("Hello from Firebase!");
});

And, this is the function call in Flutter

onTap: () async {
  try {
    final result = await FirebaseFunctions.instance
        .httpsCallable('helloWorld')
        .call();
    print(result);
  } catch (err) {
    print(err);
  }

I am getting

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:648:7)

If I use html link on browser, it works fine. How can I execute function calls in Flutter?


Solution

  • You're mixing up HTTP functions and callable functions. Your client code is expecting to invoke a callable function called "helloWorld", but you've implemented it as an HTTP function instead. These two types of functions are not compatible with each other. If you want to invoke a callable function from your app, then you need to write a callable function using onCall instead of onRequest. I suggest reviewing the examples in the linked documentation about that.

    (On the other hand, if you need to write an HTTP function, don't try to invoke it with httpsCallable in the client. Use a normal HTTP client instead.)

    See also: