instanceFor static method

FirebaseFirestore instanceFor({
  1. required FirebaseApp app,
  2. String databaseId = DatabaseId.defaultDatabaseId,
  3. OpenDatabase? openDatabase,
  4. Settings? settings,
})

Gets the instance for app and databaseId, creating it on the first call. Later calls return the same instance until it is terminated.

databaseId is the id of the database in the project, (default) unless you use a named database.

settings are those of the call that creates the instance (the defaults of Settings when null). A later call without settings, or with equal settings, returns the existing instance; a later call with different settings throws a StateError, as the other SDKs do (initializeFirestore called twice with different options in the JS SDK, settings changed after the start on Android): terminate the instance first to get one with other settings.

openDatabase opens the SQLite database of the persistent cache. Without it the cache is in memory, whatever the settings say, and is lost when the instance is terminated. It only applies to the call that creates the instance.

Returns at once, as the other SDKs do: the instance opens its cache and connects in the background, and every operation waits for that. If opening fails (for example openDatabase throws), the operations of the instance fail with that error. Throws an ArgumentError if the options of app have no projectId.

The requests are authenticated with the token provider registered on app (for example by firebase_auth_providers_dart), and are sent without a user otherwise.

// The default database, a memory cache, the production backend.
final FirebaseFirestore firestore = FirebaseFirestore.instanceFor(app: app);

// A named database, against the emulator.
final FirebaseFirestore other = FirebaseFirestore.instanceFor(
  app: app,
  databaseId: 'my-database',
  settings: Settings().withEmulator('127.0.0.1', 8080),
);

Implementation

static FirebaseFirestore instanceFor({
  required FirebaseApp app,
  String databaseId = DatabaseId.defaultDatabaseId,
  OpenDatabase? openDatabase,
  Settings? settings,
}) {
  final FirestoreMultiDbComponent component = FirestoreMultiDbComponent(
    app,
    app.authProvider,
    settings,
    appCheckProvider: app.appCheckProvider,
  );
  return component.get(databaseId, openDatabase);
}