FirebaseDatabase class final
A connection to one Realtime Database of a FirebaseApp.
Each constructor call makes its own instance with its own connection: there is no global cache, and two
instances on one URL are two connections. Settings (emulator, persistence, logging) are constructor
parameters; there are no setters. Construction opens no connection: the first operation that
needs the server (a read, a listen, .info included, a write, a transaction, an onDisconnect
request, Query.startKeepingSynced) or goOnline starts it. While connected or waiting to reconnect,
its timers keep a CLI or server program alive until close. Auth and App Check tokens come
from app.
final FirebaseDatabase db = FirebaseDatabase(app);
db.ref('rooms/1/messages').onChildAdded.listen((DatabaseEvent e) => print(e.snapshot.value));
await db.ref('rooms/1/messages').push().set(<String, Object?>{'text': 'hello'});
await db.close();
Constructors
- FirebaseDatabase(FirebaseApp app, {String? databaseUrl, DatabasePersistence? persistence, DatabaseLogger? logger})
-
Creates the database of
app. - FirebaseDatabase.emulator(FirebaseApp app, {String host = 'localhost', required int port, String? databaseUrl, String? mockUserToken, DatabasePersistence? persistence, DatabaseLogger? logger})
-
Creates the database of
appon the Database emulator athostandport. - FirebaseDatabase.test(FirebaseApp app, {String? databaseUrl, String? emulatorHost, String? mockUserToken, DatabasePersistence? persistence, DatabaseLogger? logger, DateTime clock()?, double random()?})
-
Creates a database like the main constructor, with the
clock(the wall clock) and therandomsource (a value in [0, 1), used for push ids and the reconnect backoff) replaced, so a test is deterministic.emulatorHost('host:port'or'[ipv6]:port') selects the emulator as thehostandportof FirebaseDatabase.emulator do, andmockUserTokenneeds it. The other parameters are those of the main constructor.
Properties
- app → FirebaseApp
-
The app whose options, Auth and App Check this database uses. When the app is deleted, the database
closes itself.
final
- databaseUrl → Uri
-
The URL of the database, normalised:
https://<host>with the host in lowercase and without the default port, followed by?ns=<namespace>when the host does not name the namespace (a custom domain, or a?ns=that differs from the first host label). With an emulator it ishttp://<host>:<port>?ns=<namespace>(httpsfor*.cloudworkstations.dev), whatever host the constructor'sdatabaseUrlargument names: FirebaseDatabase.emulator withhost: '10.0.2.2', port: 9000anddatabaseUrl: 'http://localhost:9000?ns=demo'giveshttp://10.0.2.2:9000?ns=demo. Every spelling of one production URL with a host name gives the same value. A loopback host and an emulator host are kept as written (lowercased), solocalhost,127.0.0.1,[::1]and[0:0:0:0:0:0:0:1]give four values for one server. Two databases can share one value on a*.cloudworkstations.devhost: a production database with?ns=and the emulator at port 443 with that namespace. It keys the persisted cache (pass it toclearSqlitePersistence).no setter - estimatedServerTime → DateTime
-
The device's time corrected by serverTimeOffset.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether close was called or app was deleted (from the moment
app.delete()is called, before the close it starts has run).no setter - isConnected → bool
-
The current value of onConnected, synchronously. Always false once close has been called
or the app deleted (from the moment
app.delete()is called, before the close it starts has run), whatever it was before.no setter -
onConnected
→ Stream<
bool> -
Whether the client is connected to the server (
.info/connected). The first event is the current state.no setter - persistenceEnabled → bool
-
Whether data written now survives a restart.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- serverTimeOffset → Duration
-
The server's clock minus this device's (
.info/serverTimeOffset); zero until the first connection.no setter
Methods
-
close(
) → Future< void> - Closes the instance. Idempotent: a second call returns the same Future.
-
goOffline(
) → void -
Disconnects from the server and stays offline until goOnline. Writes stay queued and listeners
keep their data. Synchronous and idempotent, valid before the first operation (that operation
then does not connect); a no-op after close, after
app.delete()is called, or after a failed start. -
goOnline(
) → void -
Reconnects after goOffline. On an instance that has not connected yet it starts the
connection, as a first read or write would. Synchronous and idempotent; a no-op after close,
after
app.delete()is called, or after a failed start. It does not undo a server kill: once the server has killed the connection the instance stays offline until it is recreated. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
purgeOutstandingWrites(
) → Future< void> - Cancels every write the server has not acknowledged, and every running transaction.
-
ref(
[String path = '']) → DatabaseReference -
The reference at
path.'','a/b'and'/a/b'are accepted and empty segments are dropped ('a//b'isa/b). The.infopaths work too, for exampleref('.info/connected').onValue, but writes under.infothrow an ArgumentError. An invalid key throws an ArgumentError. Makes no request, and still works after close. -
refFromUrl(
String url) → DatabaseReference -
The reference for a full
urlof this database (for examplehttps://my-project-default-rtdb.firebaseio.com/a/b). Path segments are percent-decoded. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
emulatorFromEnvironment(
FirebaseApp app, {String? databaseUrl, String? mockUserToken, DatabasePersistence? persistence, DatabaseLogger? logger}) → FirebaseDatabase? -
A FirebaseDatabase.emulator for the
host:portin theFIREBASE_DATABASE_EMULATOR_HOSTenvironment variable (an IPv6 host in brackets), or null when it is not set, is blank or the program runs on the web. Blanks around the value are dropped.