简体   繁体   中英

Using flutter how to use more than one firebase realtime database in the same App

We are developing an application in flutter using firebase realtime database, to provide several services for different customers. I would like to have a different database for each customer using the same firebase project. As firebase support multiple database in the same project I believe is possible to implement using FirebaseDatase plugin.

I tried to set a reference to the secondary database, but I can't find a settle commando to change the instance for this database. If you are using Java or other language that uses Firebase SDK this is very simple, but I can't find a way using Flutter.

Future<DataSnapshot> getDbData(String dbChildPath) async {
DataSnapshot _objdatabase;
try {
  await FirebaseDatabase.instance
      .reference()
      .child(dbChildPath)
      .once()
      .then((DataSnapshot snapshot) {
    _objdatabase = snapshot;
    print(_objdatabase.toString());
  });
  return _objdatabase;
} catch (erroDB) {
  print(erroDB);
  return null;
}}

My code is getting data from de default database.

How do I set the URL for the secondary database instance?

Please any idea?

You can use parameters for FirebaseDatabase() as below :

String fdbUrl1 = "https://my-firebase-db-1.firebaseio.com"
String fdbUrl2 = "https://my-firebase-db-2.firebaseio.com"

final databaseReference1 = FirebaseDatabase(databaseURL:fdbUrl1).instance.reference();
final databaseReference2 = FirebaseDatabase(databaseURL:fdbUrl2).instance.reference();

Note : The above is not unit tested. It should work. Just in case not, add 'app' parameter of the constructor.

A different approach would be to have your customer id in your children path, like:

await FirebaseDatabase.instance
  .reference()
  .child(customer_id)
  .child(dbChildPath)
  .once()
  .then((DataSnapshot snapshot) {

Although this is not a single DB for each customer.

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