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

两盒软妹~` 提交于 2021-01-27 12:05:06

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/57318094/using-flutter-how-to-use-more-than-one-firebase-realtime-database-in-the-same-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!