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]

Saier
  • 1

1 Answers1

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.

Sam
  • 2,972
  • 6
  • 34
  • 62
  • Your solution unfortunately did not work. This one https://stackoverflow.com/questions/67973536/flutter-firebase-connect-to-emulator-from-real-device 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 – Saier Jul 28 '23 at 09:19
  • Oh yes. For me also the emulator IP for functions and ip provided in code should be same. Thanks – Sam Jul 28 '23 at 14:42