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]

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Your solution unfortunately did not work. This one stackoverflow.com/questions/67973536/… 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 though
Oh yes. For me also the emulator IP for functions and ip provided in code should be same. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.