'[firebase_functions/unavailable] UNAVAILABLE Exception' using Firebase Local Emulator w/ Cloud functions
Asked Answered
N

2

0

I'm trying to use the Functions Emulator but it is causing problems when I try to use it in my Flutter app. I am currently on the free plan, but I have read that Functions for Local Emulator are available.

When I create my function (using node v2) like this:

exports.getBooks = onRequest(async (req, res) => {
...
}

and then make an HTTP request from my browser, I get the desired result. However, when I change it to

const {onCall, onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
const {getFirestore} = require("firebase-admin/firestore");
const admin = require("firebase-admin");
const app = admin.initializeApp();
const db = getFirestore(app);

exports.getBooks = onCall(async (request) => {
...
}

and then make the function call from my Flutter app, I get an UNAVAILABLE exception.

I have added the following code in my main.dart:

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);

and this code in my app:

final HttpsCallable getBooks = FirebaseFunctions.instance.httpsCallable('getBooks');
final response = await getBooks.call();
// OR without .call(): final response = await getBooks();

However, the .call() method is causing the issue.

I have added android:usesCleartextTraffic="true" to <application> in my AndroidManifest.xml, but it does not resolve the issue.

Do you have any idea on how to make this work?

Related Links

[N/A]

Nuthouse answered 15/6, 2023 at 20:13 Comment(0)
E
2

For me the problem was that the AndroidManifest.xml by default blocks all unencrypted traffic. Adding the following in my AndroidManifest.xml allowed me to access the local emulator (using the ip 10.0.2.2 in the client):

<application
....
android:usesCleartextTraffic="true"
....>

Make sure, however, to remove the above part from the AndroidManifest.xml after deploying your functions to Firebase.

Espagnole answered 3/7, 2024 at 16:7 Comment(1)
Perfect solutionCabaret
L
0

Try replacing the localhost with 10.0.2.2.

Also, if your functions are deployed in any specific region then specify the region too like follow.

FirebaseFunctions.instanceFor(region: 'us-central1')
  .useFunctionsEmulator('10.0.2.2', 5001);

FirebaseFunctions.instanceFor(region: 'us-central1').httpsCallable('getBooks');

The above changes should work.

Lynwoodlynx answered 27/7, 2023 at 0:49 Comment(2)
Your solution unfortunately did not work. This one #67974036 did. I had to change the host IP in the 'emulators' field in firebase.json to 0.0.0.0. Thank you for the help thoughNuthouse
Oh yes. For me also the emulator IP for functions and ip provided in code should be same. ThanksLynwoodlynx

© 2022 - 2025 — McMap. All rights reserved.