FirebaseDatabase constructor

FirebaseDatabase(
  1. FirebaseApp app, {
  2. String? databaseUrl,
  3. DatabasePersistence? persistence,
  4. DatabaseLogger? logger,
})

Creates the database of app.

databaseUrl defaults to the app's databaseURL, then to https://<projectId>-default-rtdb.firebaseio.com; with neither and no project id it throws an ArgumentError. An empty databaseUrl also selects that default, even when the app has a databaseURL. The URL must be the root of a database over TLS (https:// or wss://): a plain http:// URL (the message points to FirebaseDatabase.emulator), a path, user info, a port other than 443 on a host that is not loopback, a custom domain without ?ns= and an invalid namespace each throw an ArgumentError here, with or without persistence. The message does not quote the URL. A namespace given as ?ns= is lowercased and valid when it is 1 to 63 letters, digits or -, in any position: ?ns=-x- is accepted as the namespace -x-. The client does not check that the server has it.

persistence turns on disk persistence (memory only when null). If the store cannot be opened (its opener throws), nothing is thrown: the instance runs memory only, the client logs one warning, and persistenceEnabled turns false, so writes made then are not kept for a restart. logger receives log messages (silent when null).

The constructor is synchronous, also with persistence: opening the store and restoring writes happen in the background, and calls made meanwhile wait for it, and their local effects apply in call order. The order in which their requests then leave is not the call order across kinds: a listen or a get may leave before an earlier set. If the start fails after the store opened, every waiting and later call fails with that error.

The persisted cache is keyed by the app name and databaseUrl, which is normalised: every spelling of one URL with a host name shares one cache, and two namespaces on one host never do. A loopback host is kept as written (lowercased), so localhost, 127.0.0.1 and [::1] give three caches for one server, production or emulator.

FirebaseDatabase.emulator connects to the emulator; FirebaseDatabase.test replaces the clock and the random source.

Implementation

FirebaseDatabase(
  FirebaseApp app, {
  String? databaseUrl,
  DatabasePersistence? persistence,
  DatabaseLogger? logger,
}) : this.test(
       app,
       databaseUrl: databaseUrl,
       persistence: persistence,
       logger: logger,
     );