简体   繁体   中英

How do I connect to my local Realtime Database emulator in my Flutter app?

I am using the Realtime Database and Functions for my Flutter app. I've set up local emulators for both Database and Functions, so I can test Functions locally without the deployment cost. Now I want to connect my Flutter app to these local emulators, but I am having some trouble finding out how to do it.

The FlutterFire docs describes how to do this for Firestore ( https://firebase.flutter.dev/docs/firestore/usage#emulator-usage ), but not for the Realtime Database. Surely there must be a way to connect to the emulators, even though I am using the Realtime Database?

I have tried the following:

String host = Platform.isAndroid ? '10.0.2.2:4000' : 'localhost:4000';
    FirebaseDatabase database = FirebaseDatabase(
      app: Firebase.app(),
      databaseURL: host,
    );

But that gives me the error:

Unhandled Exception: PlatformException(error, Invalid Firebase Database url specified: 10.0.2.2:4000, null)

I am using the firebase_core and firebase_database packages on pub.

According to the Firebase documentation for Android, using the Realtime Database emulator requires a call database.useEmulator("10.0.2.2", 9000) . A quick search of the FlutterFire code shows no such calls, so it seems that emulator usage isn't implemented in FlutterFire yet.

I also couldn't find an open issue for it, so you might want to file a feature request.

Digging deeper into Firebase Java SDK, I've found that .useEmulator() only sets host and port for creating the databaseURL and in short is just a refactoring wrapper.

Code below should work with FlutterFire Realtime Database package:

String host = Platform.isAndroid ? 'http://10.0.2.2:9000' : 'http://localhost:9000';
FirebaseDatabase database = FirebaseDatabase(
  app: Firebase.app(),
  databaseURL: host,
);

Also, do check your firebase.json for the correct port to use. Default should be 9000 for database.

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "functions"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "port": 9099
    },
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "database": {
      "port": 9000
    },
    
    "ui": {
      "enabled": true
    }
  }
}

Following code worked for me. Its almost similar to @Florin Stan answer. Just the namespace of the database was missing.

String host = Platform.isAndroid ? 'http://10.0.2.2:9000' : 'http://localhost:9000';
dbInstance= FirebaseDatabase(
    databaseURL: '$host?ns=YOUR_DATABASE_NAMESPACE',
);

Database namespace can be found in firebase realtime database emulator UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM