使用Flutter如何在同一App中使用多个Firebase实时数据库 [英] Using flutter how to use more than one firebase realtime database in the same App

查看:33
本文介绍了使用Flutter如何在同一App中使用多个Firebase实时数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Firebase实时数据库开发Flutter中的应用程序,以为不同的客户提供多种服务.我想为每个使用相同Firebase项目的客户提供不同的数据库.由于Firebase在同一个项目中支持多个数据库,因此我相信可以使用FirebaseDatase插件来实现.

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.

我试图设置对辅助数据库的引用,但找不到解决突击队来更改此数据库的实例.如果您使用的是Java或其他使用Firebase SDK的语言,这非常简单,但是我找不到使用Flutter的方法.

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.

如何设置辅助数据库实例的URL?

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

有什么想法吗?

推荐答案

您可以为FirebaseDatabase()使用参数,如下所示:

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();

注意:上面未进行单元测试.它应该工作.以防万一,添加构造函数的"app"参数.

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

这篇关于使用Flutter如何在同一App中使用多个Firebase实时数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆