Use more than one Firebase database in an app created in Flutter

Asked

Viewed 309 times

0

Good evening Guys, I’m designing an app and I’m thinking about using flutter and Firebase. My project consists of a customer register where each company can have its customer portfolio online. Is there a way to use multiple Firebase databases in the same Flutter app? Because every time I create a new base it generates a configuration Json that I need to attach to my application. I would like to not have to use this file but rather another way to connect the bases without the file because each company will have its base, but the App will be the same for everyone. Is there any way to do that?

1 answer

0

In a quick search, it seems that it is possible to do what you want, but anyway you would have to have a . json default file. To connect to another database you would have to create another instance of FirebaseApp and configure it as you wish:

FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseOptions options = new FirebaseOptions.Builder()
       .setApplicationId("1:530266078999:android:481c4ecf3253701e") // Requirido para o Analytics.
       .setApiKey("AIzaSyBRxOyIj5dJkKgAVPXRLYFkdZwh2Xxq51k") // Requirido para o Auth.
       .setDatabaseUrl("https://project-1765055333176374514.firebaseio.com/") // Required for RTDB.
       .build();
FirebaseApp.initializeApp(this /* Context */, options, "secundaria");

And to access this new instance just specify the same at the time of declaring your connection

FirebaseApp app = FirebaseApp.getInstance("secundaria");
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);

Normally we don’t need to set anything up manually because the firebase’s browser already does everything for us, searching the data in the google-services.js configuration file. In case then you would have the default connection, which comes from the configuration file, and to connect to other databases you would make the connection manually at runtime dynamically.

Source: The Firebase Blog

I don’t have access to Flutter right now and I didn’t actually test that method, but it seems to be kind of simple.

Is there this other question in the English OS that can help you with something too

Browser other questions tagged

You are not signed in. Login or sign up in order to post.