Using multiple Firebase projects in your Flutter App 🔥🚀

Yousefalnajjar
3 min readNov 7, 2023

--

Connecting multiple different Firebase projects into one Flutter project can be a strategic decision in certain scenarios, offering various benefits.

Such a case if you have two different application and every one has his own Firebase project with data in the NOSQL Firestore and you need a shared page between those two application how can you access the Firebase Firestore data of the second project using the first application and the opposite.

In this article, I want to share with you the simplest way to integrate as much as you need of firebase projects into your project.

Instructions :

We assume that you have two different firebase projects that are connected to two different flutter application successfully and all the firebase plugins are attached to your project.

Let’s start coding !

almost every flutter project that is connected to a Firebase must start with the Firebase initialization and with the new connection between flutter application and firebase using the Firebase CLI a DefultFirebaseOption class will be created automatically and you need to call it in the start of your main like the following :

await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

after that you can use all Firebase function in the app connected to the configure firebase that is in the DefultFirebaseOption class from any where, example for the FirebaseFirestore you can create an instance of it and use it’s collection methods as following:

// Reference to the Firestore collection
CollectionReference collectionRef = FirebaseFirestore.instance.collection('collectionName');

// Retrieve documents from the collection
QuerySnapshot querySnapshot = await collectionRef.get();

So now you can access your first FirebaseFirestore from any where in the application but..

How can you access the second one?

Easy, you just need to reinitialize the other firebases, unfortunately you can’t do that using the Firebase CLI because that will override the existing one so let us see how to initialize the second firebase project manually.

In the main method and below your first initialize app you can start initializing your second Firebase by copying the Firebase Option from your second DefultFirebaseOption class or writing them manually and sitting a reference name for your second firebase as following:

await   FirebaseApp.initializeApp(
name: 'secondFirebaseName', // Give your second app a custom name
options: FirebaseOptions(
apiKey: 'YOUR_API_KEY',
authDomain: 'YOUR_AUTH_DOMAIN',
databaseURL: 'YOUR_DATABASE_URL',
projectId: 'YOUR_PROJECT_ID',
storageBucket: 'YOUR_STORAGE_BUCKET',
messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',
appId: 'YOUR_APP_ID',
),
);

after that you can use all the second firebase function in your app for example for the FirebaseFirestore you can create an instance of it by creating FirebaseApp instance connected to the second Firebase by it’s name reference and use your collection methods as following:

FirebaseApp otherFirebase = Firebase.app('secondFirebaseName');

FirebaseFirestore firestore =
await FirebaseFirestore.instanceFor(app: otherFirebase);

// Reference to the second Firestore collection
CollectionReference secondCollectionRef = firestore.collection('collectionName');

// Retrieve documents from the collection
QuerySnapshot querySnapshot = await secondCollectionRef.get();

and that’s it, now you can easily move data between your two application!

Happy Coding 💓

--

--

Yousefalnajjar
Yousefalnajjar

Written by Yousefalnajjar

I am passionate about learning new technologies and sharing my knowledge with others

Responses (1)