Kucing Malaya
Kucing Malaya

Reputation: 465

How to connect multiple Realtime Databases from the same project (Flutter)

I have created a new Realtime Database in the same project.

I followed the instructions here but got some errors.

final db = FirebaseDatabase.instance.ref(); // default database instance

// using ref()
final db2 = FirebaseDatabase.instance.ref('https://mydb2.us-central1.firebasedatabase.app');
// error Invalid Firebase Database path
// Firebase Database paths must not contain '.', '#', '$', '[', or ']'

// using refFromURL()
final db3 = FirebaseDatabase.instance.refFromURL('https://mydb2.us-central1.firebasedatabase.app');
// error Invalid argument (must equal the current FirebaseDatabase instance databaseURL)

Same goes if using .firebaseio.com in the URL.

So what is the correct way to get DatabaseReference for secondary database in the same project?

Upvotes: 3

Views: 1212

Answers (1)

Kucing Malaya
Kucing Malaya

Reputation: 465

I've figured out by using instanceFor():

final db2 = FirebaseDatabase.instanceFor(
               app: Firebase.app(),
               databaseURL: 'https://mydb2.us-central1.firebasedatabase.app/'
            ).ref();

Upvotes: 4

Related Questions