David L
David L

Reputation: 1344

How to connect a Flutter App with a Secondary Firebase Web App?

My App was developed with Flutter and Firebase RTDB (Database 1). It was build for IOS and Android.

I have a secondary App that was developed with JS and Firebase RTDB (Database 2). It was created for the Web.

Now, I would like to write to Database 2 from my Flutter App. What should I do step by step?

I tried to apply Add Multiple Projects using something like:

Future<void> _initSecondaryFirebaseDB() async {

    await Firebase.initializeApp(
    name: 'SecondaryQRApp',
    options: const FirebaseOptions(
        appId: 'APP Id 847597056ab2f26',
        apiKey: 'Api Key InXspCU',
        messagingSenderId: 'messagingSenderId 35345',
        projectId: 'projectId of Web App'
    )
);

But I'm not sure if it's a secure procedure and I don't know how to call the secondary database instance...

 DatabaseReference db2 = FirebaseDatabase.instance.reference(); 
//It wouldn't work because I have a Primary or Default Database in my Flutter App

What do I need to do step y step to connect my Flutter App with the secondary Firebase RTDB (Database 2)?

Upvotes: 1

Views: 600

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599061

When you call FirebaseDatabase.instance you're getting the default instance of the database. To get the instance for a specific configuration, you need to pass in the FirebaseApp instance for that configuration.

var app = await Firebase.initializeApp(
  name: 'SecondaryQRApp',
  options: const FirebaseOptions(
    appId: 'APP Id 847597056ab2f26',
    apiKey: 'Api Key InXspCU',
    messagingSenderId: 'messagingSenderId 35345',
    projectId: 'projectId of Web App'
  )
);
var db = FirebaseDatabase(app: app);

If you don't have the app instance ready, you can look it up by its name:

var app = FirebaseApp.apps.firstWhere(app => app.name == "SecondaryQRApp");

Upvotes: 3

Related Questions