Reputation: 51
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?
Upvotes: 5
Views: 2351
Reputation: 252
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.
Upvotes: 0
Reputation: 14215
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.
Upvotes: 5