Het Patel
Het Patel

Reputation: 19

Firebase RTDB Flutter app: Multiple Databases

So, I am using the Blaze plan in Firebase and I have multiple realtime databases. I was wondering how I can connect to a specific one through my Flutter app.

FirebaseDatabase.instance.reference(); connects me to the default database, but how can I connect to another one?

Upvotes: 0

Views: 798

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

Using FirebaseDatabase.instance is just a shorthand notation to getting the default FirebaseApp instance, which is typically auto-initialized from the values in your google-services.json/google-services.info.plist

You can explicitly initialize a FirebaseApp with configuration data in your code. A snippet of the relevant code from an app I happen to be working on:

  final FirebaseApp  app = await FirebaseApp.configure(
    name: "defaultAppName",
    options: Platform.isIOS
        ? const FirebaseOptions(
            googleAppID: '....',
            gcmSenderID: '...',
            databaseURL: '...',
          )
        : const FirebaseOptions(
            googleAppID: '...',
            apiKey: '...',
            databaseURL: '...',
          ),
  );*/
  FirebaseDatabase(app: app).setPersistenceEnabled(true);
  FirebaseDatabase(app: app).reference().child("/rounds/r1").orderByValue().onChildAdded.forEach((event) => {
    print(event.snapshot.value)
  });

Upvotes: 2

Related Questions