1

I use firebase emulator and my cloud functions are in error...

I call my function like that :

HttpsCallable displayNameAndEmail = FirebaseFunctions.instanceFor(region: 'europe-west1').httpsCallable("displayNameAndEmail");

displayNameAndEmail.call({
          'displayName': "$firstName $lastName",
          'lang': lang,
          'email': email
        });
        print("\nDisplayName6\n\n");

And here is the error I get :

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: [firebase_functions/unavailable] UNAVAILABLE
E/flutter (14049): 
E/flutter (14049): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7)
E/flutter (14049): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
E/flutter (14049): <asynchronous suspension>
E/flutter (14049): #2      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:22:24)
E/flutter (14049): <asynchronous suspension>
E/flutter (14049): #3      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:34:37)
E/flutter (14049): <asynchronous suspension>
E/flutter (14049): 
E/flutter (14049): #0      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:38:7)
E/flutter (14049): <asynchronous suspension>
E/flutter (14049): #1      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:34:37)
E/flutter (14049): <asynchronous suspension>
E/flutter (14049): 

Any idea ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Simon B
  • 503
  • 12
  • 29
  • Did you call `useFunctionsEmulator` as shown here: https://stackoverflow.com/questions/62994836/how-can-i-call-local-firebase-functions-in-emulator-from-flutter? – Frank van Puffelen Jun 20 '21 at 17:31
  • Yes I did... @FrankvanPuffelen – Simon B Jun 20 '21 at 17:44
  • Thanks for confirming. Instead of saying that in a comment however, can you edit your question to show how you did that? We'll probably also need to see the minimal implementation of the Cloud Function with which you get this error, so please add that to your question too. – Frank van Puffelen Jun 20 '21 at 18:45

2 Answers2

1

The first line creates a separate FirebaseFunctions instance (for the specified region). You have to instruct this new instance to connect to the emulator as Frank pointed out in the first comment.

Here is the FlutterFire documentation providing more details. If you are on Android:

  • remember to use 10.0.2.2 as the host (necessary when using the Android emulator)
  • add
<application android:usesCleartextTraffic="true"/>

to android\app\src\debug\AndroidManifest.xml. This allows plain HTTP traffic to/from the Firebase emulators.

Another useful mention is to leverage the dart cascade notation, .., as follows:

String ip = Platform.isAndroid ? '10.0.2.2' : 'localhost';

var instance = FirebaseFunctions.instanceFor(region: 'europe-west1')
  ..useFunctionsEmulator(origin: 'http://$ip:5001');
HttpsCallable displayNameAndEmail = 
  instance.httpsCallable("displayNameAndEmail");

Also, if you upgrade to the latest version, the FlutterFire team seems to aim for a more uniform approach/interface. All emulator enabling methods (such as useFunctionsEmulator) now accept host and port as positional arguments (previously it took only one named argument, origin).

0

In my case, I have only used it like that. I haven't edited the XML, nor do I need to put localhost 10.0.2.2 for android. Just add your region.

  FirebaseFunctions.instanceFor(region: "us-central").useFunctionsEmulator('localhost', 5001);