4

I'm trying to call a HTTPS callable function using emulators, from my flutter application, but it seems not to work. After I call the function, it always takes around 5 seconds and responds with an error. It works completely normal if I deploy the function on the server instead.

Here is my function

exports.try = functions.https.onCall((data, context) => {
  return 1;
});

It is located at us-central1

How I started the emulators

firebase emulators:start --only functions

How I called it from the code

FirebaseFunctions functions = FirebaseFunctions.instance;
functions.useFunctionsEmulator(origin: "http://10.0.2.2:5001");
HttpsCallable callable = functions.httpsCallable('games-oneToHundred-try');
try {
  var result  = await callable();
  print(result.data);
} catch (e) {
  print(e);
}

The error is thrown is:

[firebase_functions/unavailable] UNAVAILABLE

I modified the android manifest as suggested in Error connecting to local Firebase functions emulator from Flutter app like the following:

android:usesCleartextTraffic="true"

2 Answers2

0

HTTPS isn't available for local IP, using an emulator you must use localhost

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

if you must use the IP for example on a local network with a real device, you can follow this guide https://stackoverflow.com/a/62985709/2301161

DIGI Byte
  • 4,225
  • 1
  • 12
  • 20
  • 1
    I've already tried localhost and also did what that post suggested, as you can see from my reference link. Both of those solutions doesn't work for me – Đặng Minh Hiếu Apr 25 '21 at 05:34
0

The problem was that my physical device cannot connect to localhost of my laptop. Using Android Emulator solved my problem

  • Consider listening on `0.0.0.0` from your emulator is you still want to use your real device: https://stackoverflow.com/q/67973536/2619537 – maganap Sep 17 '21 at 17:14