Flutterfire: [cloud_firestore] Will it be possible to let Flutter Firestore talk to local emulator Firestore instance?

Created on 11 Dec 2019  路  2Comments  路  Source: FirebaseExtended/flutterfire

Is your feature request related to a problem? Please describe.
I have a set of cloud functions that listen to onWrite, onDelete and few other firestore events.

Firebase supports running cloud functions & cloud firestore instances locally using emulators.

However, there is no way to test my Flutter app against those emulators, because the Flutter app using cloud_firestore always connects to the Firestore production endpoint.

Describe the solution you'd like
Similar to how native [iOS, Android, and Web] Firestore SDKs can talk to local emulator instance, I want to do the same thing with Flutter app.

enhancement

Most helpful comment

By default, Firestore emulator was listening to localhost:8080, which is only accessible from local device. I had to change that to 0.0.0.0:8080 in firebase.json file to allow Flutter running in Android Emulator to be able to connect to my dev environment.

My firebase.json file.

{
  "functions": {
      ...
  },
  "firestore": {
      ...
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "host": "0.0.0.0", // <-- Add this row.
      "port": 8080
    },
  }
}

So after I updated Firestore setting in Flutter, and updated firebase.json, I was able to connect to the local emulator firestore instance. I am closing this issue, since my requested feature is already supported.

All 2 comments

Hm.. I just found out that you can supply settings to Flutter firestore sdk.

  Firestore.instance.settings(
    host: "192.168.86.1:8080",  //Using lan address since `localhost` points to Android Emulator itself.
    sslEnabled: false,
    persistenceEnabled: false,
  );

However, after setting above in main.dart, I am only seeing connection refused error like below when I start the app.

W/Firestore(30139): (21.3.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
W/Firestore(30139): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
E/flutter (30139): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(Error performing get, Failed to get document because the client is offline., null)
E/flutter (30139): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (30139): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (30139):
E/flutter (30139): #2 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:344:48)
E/flutter (30139):
E/flutter (30139): #3 DocumentReference.get (package:cloud_firestore/src/document_reference.dart:84:33)

I am curious if connecting to custom emulator Firestore endpoint is supported in Flutter.

By default, Firestore emulator was listening to localhost:8080, which is only accessible from local device. I had to change that to 0.0.0.0:8080 in firebase.json file to allow Flutter running in Android Emulator to be able to connect to my dev environment.

My firebase.json file.

{
  "functions": {
      ...
  },
  "firestore": {
      ...
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "host": "0.0.0.0", // <-- Add this row.
      "port": 8080
    },
  }
}

So after I updated Firestore setting in Flutter, and updated firebase.json, I was able to connect to the local emulator firestore instance. I am closing this issue, since my requested feature is already supported.

Was this page helpful?
0 / 5 - 0 ratings