Skip to main content

Core

Notice

This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:

https://firebase.google.com/docs/flutter/setup

The firebase_core plugin is responsible for connecting your Flutter app to your Firebase project. The plugin must be installed and initialized before the usage of any other FlutterFire plugins. It provides basic functionality such as:

Installation#

The firebase_core plugin can be installed by following the Getting Started documentation. Once installed, import the plugin:

import 'package:firebase_core/firebase_core.dart';

Default Firebase app#

FlutterFire requires a default Firebase app to be present before initialization, otherwise an exception will be thrown. The steps for setting up a default app for your platform can be found in the Getting Started documentation.

Some plugins such as Analytics & Performance Monitoring are only compatible with the default Firebase app, however, plugins such as Authentication can take advantage of Secondary Firebase Apps, allowing you to use multiple Firebase projects at once.

To access the default app, call the initializeApp or app method on the Firebase class:

FirebaseApp defaultApp = await Firebase.initializeApp();
// or
FirebaseApp defaultApp = Firebase.app();

Secondary Firebase apps#

Some FlutterFire plugins allow the usage of secondary Firebase apps, letting you interchange the project the plugin uses. Currently, the Firebase SDKs provide functionality for using secondary apps with the following services:

  • Authentication.
  • Realtime Database.
  • Cloud Firestore.
  • Cloud Functions.
  • Cloud Storage.
  • Instance ID.
  • ML Kit Natural Language.
  • ML Kit Vision.
  • Remote Config.

Initializing secondary apps#

To initialize a secondary app, call the initializeApp method with a name and options:

await Firebase.initializeApp(
name: 'SecondaryApp',
options: const FirebaseOptions(
appId: 'my_appId',
apiKey: 'my_apiKey',
messagingSenderId: 'my_messagingSenderId',
projectId: 'my_projectId'
)
);

At a minimum, you must provide the appId, apiKey, messagingSenderId and projectId. Although the other options are not required, it is recommended you view the FirebaseOptions reference API for the full list of options available.

Accessing secondary apps#

Once initialized, secondary apps can be accessed via the app method on FirebaseCore:

FirebaseApp secondaryApp = Firebase.app('SecondaryApp');

Attempting to access an app that does not exist will throw an exception.

It is also possible to get all existing apps at once via the apps static property on Firebase class:

List<FirebaseApp> apps = Firebase.apps;
apps.forEach((app) {
print('App name: ${app.name}');
});

Using app instances#

Each FlutterFire plugin provides a streamlined approach for using the default app as well as secondary apps (if applicable). The convenient way to use the default app is by accessing the instance property on each plugin base class. For example if using Cloud Firestore:

// Access Firestore using the default Firebase app:
FirebaseFirestore firestore = FirebaseFirestore.instance;
firestore
.collection('users')
.snapshots()
.listen((QuerySnapshot snapshot) {
// Query snapshot of the user's collection on the default Firebase app
});

If instead you'd like to use a secondary app, pass it to the instanceFor static method on each plugin base class. For example if using Cloud Firestore:

FirebaseApp secondaryApp = Firebase.app('SecondaryApp');
FirebaseFirestore firestore = FirebaseFirestore.instanceFor(
app: secondaryApp
);
firestore
.collection('users')
.snapshots()
.listen((QuerySnapshot snapshot) {
// Query snapshot of the user's collection on the SecondaryApp
});

Deleting instances#

If you no longer need a secondary app, you can delete it by calling the delete method on the FirebaseApp instance:

FirebaseApp secondaryApp = Firebase.app('SecondaryApp');
await secondaryApp.delete();

Any plugin usage attempting to use a deleted app will throw an exception. The default app cannot be deleted and will throw an exception if deleted.