3

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

Lab
  • 1,237
  • 2
  • 13
  • 30
  • 1
    I found this similar [question](https://stackoverflow.com/questions/61055969/flutter-firebase-cloud-function-can-not-be-called) to yours and I think that this one could help you . As you mentioned, the issue appears when you don't specify the region, so could you please share if your cloud function is in the same region (europe-west3) that you are using to call it ? – Andie Vanille Jun 25 '20 at 16:19
  • @AndieVanille I updated the question you can see that I already put the region in the function. – Lab Jun 25 '20 at 17:12
  • I have got the same problem with a Callable function – Patrick Jun 26 '20 at 12:36

3 Answers3

5

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': 'erick@mail.com'
  });

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
Loolooii
  • 8,588
  • 14
  • 66
  • 90
Erick M. Sprengel
  • 1,921
  • 1
  • 17
  • 20
4

in my case, I have wrong callable function name

FirebaseFunctions.httpsCallable('wrong name in here')
Alexa289
  • 8,089
  • 10
  • 74
  • 178
0

You need to set region on both side.

In your function:

exports.foo = functions.region('europe-west3').https.onCall(async (data, context) => ...

In Flutter:

final functions = FirebaseFunctions.instanceFor(region: 'europe-west3');
functions.httpsCallable('foo');
iDecode
  • 22,623
  • 19
  • 99
  • 186