firebase_database_dart 0.1.0
firebase_database_dart: ^0.1.0 copied to clipboard
Unofficial pure-Dart client of the Firebase Realtime Database for the Dart VM and the web: listeners, queries, transactions, onDisconnect.
0.1.0 #
- First version. A client of the Firebase Realtime Database in Dart, for the Dart VM and the web. It speaks the
WebSocket protocol of the Firebase JS SDK, with Dart names (
DatabaseReference,onValue,runTransaction) and Dart streams. It is not affiliated with or supported by Google or Firebase. - It covers reads and writes, listeners, queries, transactions,
onDisconnect, server values and the connection state. An optional disk cache is infirebase_database_dart_sqlite. - Where it ran. Dart VM: macOS (2,645 tests of the client, 60 of them skipped; 29 tests against the Database emulator v4.11.2), Linux x64 in a Debian 13 container and Windows 11 x64 (both at an earlier commit). Web: Chrome on macOS, the whole test tree, 2,584 tests with dart2js and 2,588 with dart2wasm. Not run: a release build of a desktop app, Safari, Firefox and a real display.
- Verified against production. The verification kit has 41 checks. On 2026-11-27 all 41 ran as one run against a
dedicated database instance of one Firebase project, on the final client of this version, and all 41 passed; the
same day the kit ran on the emulator and all 41 passed there too. Earlier runs passed as well (2026-11-25 and 26,
checks 1 to 39; checks 40 and 41 each alone). The 2026-11-27 run had no
--expect-emptied-node-fix, so checks 22 and 38 (c), (e) and (f) record that the client drops the priority of an emptied node, as the JS SDK does (tool/verify_production_database/README.md, "Known results"). "Run" here means one run, not repeated runs. Some checks are informational and record what the client does. The kit signs up a throwaway user, writes about 1.1 MB below a path of its own and removes it. It covers the connection and its TLS, the server's host, every value type, server values, listeners, queries, transactions,onDisconnect, offline writes, rules with and without a user, a token refresh on a live connection, a sign-out, timings, a 1 MB value, pipelined writes, and raw frames for the server's answers about priorities and limit queries. - Never run against production: token expiry after an hour, a rules change during a connection, a 10 MiB string leaf
and a 16 MB write, App Check with a real provider, the legacy
firebaseio.comhost and a custom domain, the re-send of anincrementwhose answer was lost, and the limit-view race of section 25. Section 23 ofdocs/database_decisions.mdhas the list. - Known limits:
- Writes are delivered at least once. A re-sent
ServerValue.incrementcan apply twice; use a transaction to count. - A hung TLS handshake cannot be closed on the Dart VM. The client gives up after its 30 s connect timeout and tries again.
- A dead socket after a wake is noticed after about 46 s, and after about 75 s at worst, plus the connect (computed from the timing constants, not measured).
- A
ServerValue.incrementwhose sum leaves the range of a double is a local infinity until the server's value replaces it. - One process or isolate owns a cache file; a second one runs memory-only with one logged warning.
- Transactions and
onDisconnectregistrations are not persisted. - Priorities and pending writes. As in the JS SDK, a node that is emptied and refilled loses its priority locally
while the server keeps it, and a transaction on a node that holds only a priority fails with
max-retries(reported upstream as firebase/firebase-js-sdk #10688; README, "Known limits", and section 25 ofdocs/database_decisions.md, table "Behaviours shared with the JS SDK that are kept", rows 2 and 8). - A write below a leaf that the server refuses shows a wrong value until the refusal arrives, 2 to 4 ms on the
emulator; the JS SDK keeps the wrong value until a listener is attached again (firebase/firebase-js-sdk #10687; the
offline form, #10686, is right in this client). Section 25 of
docs/database_decisions.md, same table, row 1. - Not in this version: long polling, web disk persistence, a REST client, an instance cache, typed converters, the
compat API and a proxy option in the API. On the Dart VM a secure connection honours
HTTPS_PROXYandNO_PROXY.
- Writes are delivered at least once. A re-sent
- Differences from the JS SDK are in the README, "How it differs from the JS SDK", and in section 25 of
docs/database_decisions.md(38 rows, each with the test that pins it). FirebaseDatabase(app, databaseUrl:, emulatorHost:, mockUserToken:, persistence:, logger:): one instance is one connection; configuration is constructor-only and there is no instance cache. A new instance opens no connection: the first operation that needs the server (a read, a listener, a write, a transaction, anonDisconnectcall,keepSynced(true)) orgoOnline()does. Persistence still opens at construction.- The constructor refuses, before any connection, a database URL with a path, a custom domain without
?ns=, a port other than 443 off loopback, an invalid project id and plainhttp://outside the emulator; a start that fails fails every pending and later call.refFromUrlrefuses user info, plain http, other ports and extra query keys. The disk cache key is the normalised database URL. - Reads and writes:
get,once,set,update,push,setPriority,remove,refFromUrl;DataSnapshot.valueis plain, unmodifiable Dart data. Aget()with a range or a limit onorderByChildororderByValueneeds.indexOnin the rules; an order-only one does not. DataSnapshot.toJson()(the export form) reads numbers asvalueandprioritydo: a value or priority the server stores as1.0exports as theint1 on every compiler, as JSexportVal()gives1.- Listening:
onValue,onChildAdded,onChildChanged,onChildMoved,onChildRemovedandonChildEvents, one ordered stream of the four child events. - Queries:
orderByChild,orderByKey,orderByValue,orderByPriority,startAt,startAfter,endAt,endBefore,equalTo,limitToFirst,limitToLast,keepSynced. runTransactionwithTransaction.successandTransaction.abort: the first run of the handler happens inside the call, its events are queued, and a sent transaction that loses its connection fails withdisconnected;onDisconnectwithset,update,removeandcancel;ServerValue.timestampandServerValue.increment.- Connection state:
onConnected,isConnected,serverTimeOffset,estimatedServerTime,goOffline,goOnline,purgeOutstandingWrites(cancels writes not yet acknowledged andonDisconnectoperations still queued locally); a dead-link probe, and reconnect with a 30 s cap. A server message the client cannot accept closes the connection and reconnects with the delay at its cap; JSON deeper than 64 levels is refused before decoding. - A secure connection on the Dart VM honours
HTTPS_PROXYandNO_PROXY(and the lower-case forms); loopback and the emulator are always direct; there is no API option. - ID and App Check tokens from the app's providers, sent only over TLS or to the emulator; a change event of the
auth provider is a signal and the client fetches the token (an empty token means signed out);
mockUserTokenForfor the emulator. FirebaseDatabaseExceptionwith the codespermission-denied,disconnected,max-retries,overridden-by-set,unavailable,network-error,write-canceled,index-not-defined,operation-failed,too-big,database-closed,data-staleandunknown. For a server status the client does not know,unknowncarries the server's reason as its message andstatus: reasonasserverStatus, as for the other codes; aget()fails the same way as a write. Aget()whose answer the client cannot read fails withunknown, a fixed message, the original error ascauseand noserverStatus.- Closing the database or deleting its app ends every stream with done and no error; pending Futures fail with
database-closed. A server refusal ends a stream with an error; losing the connection never does. - Logging through a callback, off by default; credentials are never logged.
- Optional disk persistence through
firebase_database_dart_sqlite; only data under a listener orkeepSyncedis written to the cache; an offlineget()answers from the cache only when it holds the complete value for the query;cacheServerData: falsekeeps only the pending writes.package:firebase_database_dart/storage_engine.dartis the library for custom persistence engines. It names only types it exports: an engine stores a query as its path andQuerySpec.toWireObject()and rebuilds it withQuerySpec.fromPathAndWireObject;Index,KeyIndexandNamedNodeare exported there; theQuerySpec(path, params)andImmutableTree(value, children)constructors,QuerySpec.params,QuerySpec.toListenSpecandImmutableTree.childrenare@internal. package:firebase_database_dart/testing.dart: a fake server for tests of your own code. Breaking for tests that acked the newest write first:FakeDatabaseServer'sackLastWrite,failLastWriteandackLastOnDisconnectnow throw aStateErrorwhen the newest request is not the oldest unanswered one, because a test must answer writes andonDisconnectrequests in the order they left (after a reconnect, puts and merges beforeonDisconnectrequests made offline, as the client sends them). PassoutOfOrder: trueto keep the old behaviour; the newackNextWrite,failNextWriteandackNextOnDisconnectanswer the oldest.- The event dispatcher yields to the event loop after 1,000 events with an unthrottled task in the browser, so a burst of events in a hidden tab is delivered in bounded time.
- A limit query under a listener at or above its path keeps the right window when one update by another client moves a child out of it: the child that enters holds the update's earlier changes, and the window ends as the server's. The JS SDK showed a stale or removed child there until every listener at the path was removed.
updateandOnDisconnect.updatethrow anArgumentErrorat the call for an object, at any depth, with a non-null priority and no value once nulls and empty objects are dropped ({'n': {'.priority': 5}},{'n': {'.value': null, '.priority': 5}},{'n': {'.priority': 5, 'a': {}}}): the server answers such a merge with an error that names no request, so its Future would never complete.update({'.priority': p}), a'n/.priority'key,{'.value': v, '.priority': p}and empty objects (removals) are accepted as before;setand transactions are unchanged and leave such an object out.