12

I would simply like to know how to add multiple Firebase projects to my flutter app, because I have data in two separate projects.

3 Answers 3

4

This code was able to make a connection to the Cloud Firestore:

  Future<Firestore> _connectFirestore() async {
    final FirebaseApp sharedApp = await FirebaseApp.configure(
      name: 'shared-project',
      options: FirebaseOptions(
        apiKey: 'blah-blah',
        googleAppID: 'blah-blah-blah',
        projectID: 'shared-project-id',
      ),
    );
    return Firestore(app: sharedApp);
  }

However, once I lock down the database to require authentication, now I get a PlatformException(Error 7, FIRFirestoreErrorDomain, Missing or insufficient permissions.). I am struggling to ensure that my Cloud Firestore requests are authenticated correctly.

Sign up to request clarification or add additional context in comments.

Comments

3

You need to call FirebaseApp.configure() for each project that you want to add. This works the same as FirebaseApp.initializeApp() on other platforms, so you should be able to use their documentation if it's not self explanatory.

3 Comments

Please feel free to use the "send feedback" button at the top of every firebase doc page to explain your observations.
Do I need to add the config files for each project to my app as well?
2

Steps which worked for me to add 2nd Firebase Project:

  • refactor the firebase_options.dart name.
  • in a console, run the flutterfire configure command to generate a second firebase_options.dart file (we choose the 2nd Firebase project we want to add)
  • Change the class name inside of the new file from DefaultFirebaseOptions to SecondaryFirebaseOptions

and then, after initializing the first FirebaseApp we can add:

await Firebase.initializeApp(options:SecondaryFirebaseOptions.currentPlatform, name: "secondary");
FirebaseApp secondaryApp = Firebase.app("secondary");
FirebaseFirestore firestore = FirebaseFirestore.instanceFor(app:secondaryApp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.