0

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)
Schotti100
  • 93
  • 1
  • 9
  • Do you have Google play service installed on Emulator ? – Hardik Vaghani Nov 19 '21 at 13:19
  • yes Google play service is installed – Schotti100 Nov 19 '21 at 20:14
  • Does this answer your question? [Firebase Cloud Function: & Flutter: Cannot use https callable function on emulators \[firebase\_functions/unavailable\] UNAVAILABLE](https://stackoverflow.com/questions/67246536/firebase-cloud-function-flutter-cannot-use-https-callable-function-on-emulat) – Lluís Muñoz Nov 25 '21 at 15:19
  • No i´m working already on an android emulator and changing the ip to localhost didn´t help either – Schotti100 Dec 03 '21 at 09:09
  • I´m still facing the same problem. Does no one have an idea or suggestion? – Schotti100 Dec 06 '21 at 07:31
  • According to [this documentation](https://firebase.google.com/docs/reference/android/com/google/firebase/functions/FirebaseFunctionsException.Code#public-static-final-firebasefunctionsexception.code-unavailable) this should be a transient issue. However the fact that you are still facing it and that it only happens in the emulator makes me think that the root cause of your issue might be in the configuration of your emulator. Could you please explain how you configured the emulator? – Lluís Muñoz Dec 06 '21 at 16:49
  • I followed the official documentation. (https://firebase.google.com/docs/emulator-suite/install_and_configure) I´m not sure about your question, is that what you meant or something else? – Schotti100 Dec 08 '21 at 07:43
  • Yes, that's what I meant, thanks. I suggest implementing a retry policy with exponential back off as suggested in the [documentation about the error](https://firebase.google.com/docs/reference/android/com/google/firebase/functions/FirebaseFunctionsException.Code#public-static-final-firebasefunctionsexception.code-unavailable). Additionally, you can try to listen to `0.0.0.0` as suggested in [this answer](https://stackoverflow.com/a/67975075/12512650). Lastly, I'd like to encourage you to update the question to include everything discussed in the comments. – Lluís Muñoz Dec 08 '21 at 11:06
  • I tried both now. Neither the retry policy or listening to 0.0.0.0 as in the other question changed anything. Still getting the unavailable exception everytime i call the method. – Schotti100 Dec 13 '21 at 07:58
  • Perhaps this is a silly question and I'm too many layers deep in documentation but... What's `parameters`? My understanding is that you're passing parameters from a search bar in order to get a book's data according to what's in the bar, but I may be wrong. Also, someone mentioned the transient issue of lack of availability, but I'm pretty sure if it were that error the `details` param wouldn't be empty. That said... Have you tried debugging the functions using `emulators:start --inspect-functions` and applying breakpoints to the function code? Might reveal some more info. – fabc Dec 24 '21 at 14:00
  • Oh, I should clarify that by "What's parameters" I meant "What values are you passing in paramaters, and in what format?", but there's a character limit to comments – fabc Dec 24 '21 at 14:02
  • I pass in the param: Map param = {"title": title, }; Thanks for the tipp with the --inspect-functions, didn´t know that before. I´m going to try it within the next few days. I will add the result as soon as possible. – Schotti100 Dec 26 '21 at 12:21
  • Hmmm, I have a few ideas as to what else you can look into next: In [Swift](https://firebase.google.com/docs/functions/callable#call_the_function), parameters for a call function are listed as an array and not a map, as opposed to this doc on [Dart](https://pub.dev/documentation/cloud_functions/latest/cloud_functions/HttpsCallable/call.html) - might be some odd behaviour there... I also found [this](https://stackoverflow.com/questions/62984527/error-connecting-to-local-firebase-functions-emulator-from-flutter-app), which isn't likely to be the case given the error, but it doesn't hurt to ask – fabc Dec 30 '21 at 11:13
  • I tried debugging the function with the --inspect-functions feature, but couldn´t get it to work properly. I followed this insturctions: [Click me](https://medium.com/firebase-developers/debugging-firebase-functions-in-vs-code-a1caf22db0b2). But i figured out something different, when calling the function with PostMan i get a respons that the function started. I don´t get this when calling the function from the app.... – Schotti100 Dec 30 '21 at 21:59
  • As for the suggestion with the different parameter, what do you mean exactly? I tried to create differnt parameters as an array. But Dart does not allow something like var par = ['title': "harryPotter"]; The other idea...i´m trying to set up Crashlytics maybe i get more information from there... – Schotti100 Dec 30 '21 at 22:04
  • So... No network errors (so far), I apologize about the parameters, I've little to no experience with Dart. Calling the function with PostMan is calling the emulated function, right? I know it's silly but I feel I gotta ask otherwise I'll feel even sillier if it turns out to be that and I didn't think of it. Lastly, any extra info from Crashlytics? – fabc Jan 05 '22 at 16:07

1 Answers1

2

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

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Schotti100
  • 93
  • 1
  • 9