use static method

void use({
  1. TokenProviderFactory? tokenProviderFor,
  2. AppCheckProviderFactory? appCheckProviderFor,
  3. DatabasePersistenceFactory? persistenceFor,
  4. bool? useDartCore,
})

Makes firebase_database_dart the implementation of firebase_database on any platform. Call it at the start of main, before Firebase or FirebaseDatabase is used. Calling it again replaces the whole configuration: tokenProviderFor, appCheckProviderFor and persistenceFor left out of the later call are unset, not kept from the earlier one. A Dart core an earlier call installed stays installed.

  • tokenProviderFor returns the token provider of the connection of a FlutterFire app: FirebaseAuthDart.userAuthFor(app) of firebase_auth_dart_platform, or a FlutterFireAuthTokenProvider of cloud_firestore_dart_flutterfire. Called when a client is created and the firebase_app_dart app of its FlutterFire app has no token provider yet: while it returns null, for every client created (each database, each new client after a delete); once it returned a provider, not again for that firebase_app_dart app. The Dart core of Linux deletes that app with the FlutterFire app, so a delete and create calls it again; the native and web cores keep it, with its provider. Without it, or when it returns null, the connection carries no user. The provider is installed as a TokenOnChangeProvider, whose change events carry the token. A firebase_app_dart app that already exists with the name of the FlutterFire app keeps its own providers, and gets these only where it has none.
  • appCheckProviderFor returns the App Check token provider of the connection of a FlutterFire app (FlutterFireAppCheckProvider of cloud_firestore_dart_flutterfire for the official firebase_app_check). Called as tokenProviderFor is, when the firebase_app_dart app has no App Check provider yet.
  • persistenceFor makes the clients keep their cache and pending writes on disk (see DatabasePersistenceFactory); without it every client is in memory. FirebaseDatabase.setPersistenceEnabled(false) turns it off for an instance; setPersistenceEnabled(true) without it prints a warning and stays in memory.
  • useDartCore replaces the implementation of firebase_core with a small pure-Dart one, only while firebase_core has its default method-channel implementation (a core another package installed is kept). By default (null) that happens only on Linux; pass true to do it on any platform (for example in tests), false to never do it.

Throws a StateError once an instance has created its client: the configuration is read then.

Implementation

static void use({
  TokenProviderFactory? tokenProviderFor,
  AppCheckProviderFactory? appCheckProviderFor,
  DatabasePersistenceFactory? persistenceFor,
  bool? useDartCore,
}) {
  if (DartFirebaseDatabase.anyStarted) {
    throw StateError(
      'FirebaseDatabaseDart.use must be called before the first use of '
      'FirebaseDatabase.',
    );
  }
  DartFirebaseDatabase.config = DartDatabaseConfig(
    tokenProviderFor: tokenProviderFor,
    appCheckProviderFor: appCheckProviderFor,
    persistenceFor: persistenceFor,
  );
  installCoreIfNeeded(force: useDartCore);
  if (!isRegistered) {
    final DartFirebaseDatabase platform = DartFirebaseDatabase();
    DatabasePlatform.instance = platform;
    _registered = platform;
  }
}