Search code examples
firebaseflutterhttpsgoogle-cloud-functionsfirebase-tools

Firebase Functions https call does not work on emulator


When executing the https call on the firebase emulator i only get a FirebaseFunctionException. Every other function, except the https call, works fine. When connecting to firebase without the emulator everything works as well. I can´t find a solution for this on the internet, can someone please help?

Method call:

    try {
      HttpsCallable callable =
          FirebaseFunctions.instance.httpsCallable('getBookData');
      final results = await callable.call(parameters);

Init:

WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

...

 final localHostString = Platform.isAndroid ? '10.0.2.2' : 'localhost';

    // dotenv.get('HOST_IP');

    FirebaseFunctions.instance
        .useFunctionsEmulator('http://$localHostString', 5001);

    FirebaseFirestore.instance.settings = Settings(
      host: '$localHostString:8080',
      sslEnabled: false,
      persistenceEnabled: false,
    );

    await auth.FirebaseAuth.instance.useEmulator(
        Platform.isAndroid ? 'http://localhost:9099' : 'http://0.0.0.0:9099');
  }

Exception:

code:"unavailable"
details:null
message:"UNAVAILABLE"
plugin:"firebase_functions"
stackTrace:#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:23:24)
<asynchronous suspension>
#3      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:35:37)
<asynchronous suspension>
#4      BookRequest._requestBooksFromCloudFunction (package:bookR/shared/bookRequest.dart:47:23)
<asynchronous suspension>
#5      BookRequest.getMoreBooksByTitle (package:bookR/shared/bookRequest.dart:23:24)
<asynchronous suspension>
#6      BookRequest.getBookInformationByTitle (package:bookR/shared/bookRequest.dart:37:13)
<asynchronous suspension>
#7      SearchBar.build.<anonymous closure> (package:bookR/widgets/searchBar.dart:34:28)
<asynchronous suspension>
hashCode:439854717
runtimeType:Type (FirebaseFunctionsException)

Solution

  • I finally figured it out! :D In case somebody finds this in the future:

    FirebaseFunctions.instance
            .useFunctionsEmulator('http://$localHostString', 5001);
    

    This causes the error. The correct version is:

    FirebaseFunctions.instance
            .useFunctionsEmulator('$localHostString', 5001);
    

    Now erverthing works as expected