firebase_database_dart_platform 0.1.0
firebase_database_dart_platform: ^0.1.0 copied to clipboard
Unofficial pure-Dart implementation of the FlutterFire Realtime Database platform interface: the official firebase_database API on Linux, or on any platform you choose.
firebase_database_dart_platform #
An unofficial implementation of the platform interface of the official
firebase_database Flutter plugin (13.1.0,
firebase_database_platform_interface 0.4.3+10), on top of firebase_database_dart, a pure-Dart client of the
Firebase Realtime Database over its WebSocket protocol. An app keeps the official API
(FirebaseDatabase.instance.ref('notes').onValue, set, runTransaction, onDisconnect(), ...) and the Dart
client does the work underneath, without the native Firebase SDKs.
It is for a Flutter app on Linux desktop, where firebase_database and firebase_core have no implementation of
their own: FlutterFire has no Linux plugin for firebase_database, so this package is the only implementation there,
and adding the dependency registers it. On Windows FlutterFire has its own plugin (built on the Firebase C++ SDK); an
app that lists this package as a direct dependency gets this package in its place. On the other platforms it is an
opt-in through FirebaseDatabaseDart.use(). It is not affiliated with or endorsed by Google or Firebase.
Status: 0.1.0, first version (differences). What ran and what did not:
- The client underneath,
firebase_database_dart, passes its own suites (2,645 tests on the Dart VM) and its tests against the Database emulator.- This package has 328 tests, 5 of them skipped without the emulator (another session's run, 2026-11-27). The unit tests run against the client's fake server (
package:firebase_database_dart/testing.dart).- FlutterFire's own
firebase_databaseintegration suite (77 tests) is the acceptance gate (tool/test_flutterfire_database_gate.sh, against the Database emulator): 76 pass and 1 skips itself, on macOS with the host test runner. It has not run on Linux or Windows.- On Windows 11 x64 (Flutter 3.47.8, debug) the Windows example app, written against
firebase_databasealone, builds with this package (the generated plugin registrant callsFirebaseDatabaseDart.registerWith()) and passes its 4 integration tests against the Database emulator.- On Linux x64 (a Debian 13 container) the Linux example app builds and passes its 4 integration tests under a virtual display.
- An independent review of the adapter found 2 high, 4 medium and 6 low findings; all were fixed with a test each.
- Not run: a release build of a desktop app, a real display, a production project through this adapter (the client underneath ran its verification kit against production once; see its README).
How to enable it #
dependencies:
firebase_core: ^5.1.0
firebase_database: ^13.1.0
firebase_database_dart_platform: ^0.1.0
On Linux nothing else is needed for the emulator or open rules: Flutter calls FirebaseDatabaseDart.registerWith()
before main(). Pass the options to Firebase.initializeApp (there is no native configuration file to read them
from), with a databaseURL, or at least a projectId, from which the client derives
https://<projectId>-default-rtdb.firebaseio.com as the native SDKs do:
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FirebaseDatabase.instance.ref('notes/today').set('Hello');
On Windows, list the package as a direct dependency (as above): it then replaces FlutterFire's native
firebase_database plugin (the C++ SDK), which leaves the build, and registers before main() as on Linux: the
generated plugin registrant calls FirebaseDatabaseDart.registerWith(). firebase_core stays native.
On any other platform, call FirebaseDatabaseDart.use() at the start of main(), before the first use of
Firebase or FirebaseDatabase. There firebase_core keeps its native implementation, and the native Realtime
Database SDK is still built into the app, unused.
The signed-in user #
The connection authenticates with the user that tokenProviderFor returns, as with the Firestore and Storage
platform packages of this repository. With firebase_auth_dart_platform:
FirebaseDatabaseDart.use(
tokenProviderFor: (FirebaseApp app) => FirebaseAuthDart.userAuthFor(app),
);
With the official firebase_auth (its native implementation), through cloud_firestore_dart_flutterfire:
FirebaseDatabaseDart.use(
tokenProviderFor: (FirebaseApp app) =>
FlutterFireAuthTokenProvider(FirebaseAuth.instanceFor(app: app)),
);
A sign-in, a switch of users and a token refresh re-authenticate the open connection without dropping it. A change
event of the provider is a signal: the client fetches the token from the provider, and an empty token means signed out.
The change events of FlutterFireAuthTokenProvider do not hold the token, so the provider is installed behind a
wrapper that answers each change with the token of getAccessToken; the adapter never forwards a placeholder as a
token.
Without it the connection carries no user, which is enough for the emulator and for open rules. When another platform
package of this repository already set up the app (a firebase_app_dart app with the name of the FlutterFire app), that
app keeps its token provider and tokenProviderFor is not called; when that app has no token provider, it gets the one
of tokenProviderFor. The wrapper is installed only on a provider of tokenProviderFor; for a provider that another
package installed on the shared app, the client fetches the token itself on each change event.
App Check #
Pass appCheckProviderFor with the FlutterFireAppCheckProvider of
cloud_firestore_dart_flutterfire, built on the official
firebase_app_check, as for the Storage platform package:
FirebaseDatabaseDart.use(
appCheckProviderFor: (FirebaseApp app) {
final FirebaseAppCheck appCheck = FirebaseAppCheck.instanceFor(app: app);
return FlutterFireAppCheckProvider(
getToken: appCheck.getToken,
onTokenChange: appCheck.onTokenChange,
);
},
);
Without it, or when it returns null, the connection sends no App Check token.
Options of use #
| Argument | Default | What it does |
|---|---|---|
tokenProviderFor |
none: no user | Returns the token provider of a FlutterFire app; called when a client is created and the app has no token provider yet (below). |
appCheckProviderFor |
none: no App Check token | Returns the App Check token provider of a FlutterFire app; called as tokenProviderFor is. |
persistenceFor |
none: memory only | A DatabasePersistenceFactory: returns the client's DatabasePersistence for the cache size the app set (below). |
useDartCore |
Linux only | Installs a small pure-Dart firebase_core in place of its method-channel implementation; a core another package installed is kept. Pass true for any platform (tests), false to never do it. |
use() throws a StateError once an instance has created its client, because the configuration is read then.
Called again before that, it 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 already installed
stays). The provider factories are called when a client is created and the firebase_app_dart app of the FlutterFire
app has no provider of that kind yet: while a factory returns null, for every client created (each database, each new
client after a delete); once it returned a provider, not again for that app. The Dart core of Linux deletes the
firebase_app_dart app with the FlutterFire app, so a delete and create calls the factories again; the native and web
cores keep that app and its providers.
FirebaseDatabaseDart.clientFor(app) returns the client behind an instance, for what the official API does not
have: onConnected, isConnected, serverTimeOffset, onChildEvents, close.
Persistence #
The clients keep their data in memory unless use was given persistenceFor, a function from the cache size to a
DatabasePersistence. The disk one comes from firebase_database_dart_sqlite (this package does not depend on it).
The function is called each time an instance (app and database URL) creates a client, at the first use and again
after the app was deleted, with the size of FirebaseDatabase.setPersistenceCacheSizeBytes, 10 MiB when the app set
none. The client keys the cache by app name and database URL, so one directory serves every app.
setPersistenceEnabled(false)turns it off for an instance;setPersistenceEnabled(true)withoutpersistenceForprints a warning and stays in memory.- With persistence, pending writes survive a restart and are sent again (at least once). Transactions and
onDisconnectregistrations do not: they are memory only. RegisteronDisconnectwrites again when the client reconnects (clientFor(app).onConnected).
Emulator, logging, deleting an app #
- The emulator is used only when the app calls
useDatabaseEmulator. TheFIREBASE_DATABASE_EMULATOR_HOSTenvironment variable is not read, as with the Firestore and Storage platform packages and FlutterFire's own plugins. - The client's warnings and errors (for example that persistence could not be opened and the client runs in memory)
are printed with
debugPrintalways; its debug and info lines only aftersetLoggingEnabled(true). FirebaseApp.delete()closes the clients of that app on every platform (throughfirebase_core'sregisterService), except a client created while a delete ran (below).- Created again under the same name with the same options, the app's
FirebaseDatabaseobjects keep working: the next call creates a new client with the settings the object had, also after a second delete and create.useDatabaseEmulatorcalled after the delete and before that call takes effect (after a use while the delete ran it throws, as after any first use).instanceFor(app: newApp)returns the same object, andclientForwith the new or the old app object returns the client that object uses. - A use while
FirebaseApp.delete()runs (afterfirebase_coredisposed the services, before the core removed the app) creates a client with the object's settings that the delete does not close on the native and web cores (shown on test cores that behave like them): the object keeps using it after the app is created again, and a later delete does not close it either. On the Dart core of Linux the delete closes it, and the next call creates a new one with the same settings. - When the
firebase_app_dartapp of the name is deleted directly and the FlutterFire app is kept, the next call creates a new client with the object's settings. - Created again under the same name with other options (any field that
FirebaseOptions ==compares;recaptchaSiteKeyis not one) while an instance of the earlier app is kept, a call that needs a new client throws aStateError("The Firebase app ... was deleted and created again with other options"), for the database the earlier app used and for any other:instanceFor(app: newApp, databaseURL: anyUrl),clientForwith the new or the old app object, with or without a URL, and the old object'sget,set,goOnline,goOfflineandpurgeOutstandingWrites(a listen ofonValuegets it as an error event). An old object used while the delete ran is the exception on the native and web cores: it keeps the client made then, with the earlier app's options and its settings, and its calls work. No client without the settings made before the delete is created.ref,useDatabaseEmulatorandsetPersistenceEnabledneed no client and do not throw. The tests show this on the Dart core and on test cores that behave like the native ones and like the web one. - Instances are never released, so after such a recreate the name throws for the rest of the process; creating the app again with the earlier options makes it work again. To use other options, give the app another name, or restart the app. A test suite that, in one process, uses a database of an app name and later creates that name again with other options meets this throw; give each test its own app name.
- An app that never had a
FirebaseDatabaseinstance before its delete has nothing kept: created again with other options, it works as a new app. With the same options, a database the earlier app did not use gets a new client of its own, without the settings of the first database.
What is implemented #
| Official API | Underneath | Notes |
|---|---|---|
FirebaseDatabase.instanceFor(app:, databaseURL:) |
one firebase_database_dart client per app and database, created at the first I/O |
instance and instanceFor with the same database (the app's URL, a trailing slash, another case of the host) share it, as on Android |
useDatabaseEmulator, setPersistenceEnabled, setPersistenceCacheSizeBytes, setLoggingEnabled |
the client's constructor arguments | captured before the client exists; see "How it differs" |
goOnline, goOffline, purgeOutstandingWrites |
the same client calls | pending writes fail with write-cancelled |
ref, refFromURL, child, parent, root, key, path |
the location's keys | no client, no I/O |
set, setWithPriority, update, setPriority, remove, push |
set, update, setPriority, push |
map keys are stringified (as FlutterFire does) |
onValue, onChildAdded, onChildChanged, onChildRemoved, onChildMoved, once, get |
the client's streams and get |
a once() pending at close or app deletion fails with StateError "No element"; get() fails with database-closed |
orderBy*, startAt, startAfter, endAt, endBefore, equalTo, limitToFirst, limitToLast, keepSynced |
the client's Query |
|
runTransaction(handler, applyLocally:) |
runTransaction |
the handler gets a fresh mutable copy on each run; it may run several times |
onDisconnect(): set, setWithPriority, update, remove, cancel |
onDisconnect |
|
ServerValue.timestamp, ServerValue.increment |
resolved by the server, and by the client for local events | |
DataSnapshot: key, value, priority, exists, child, hasChild, children, ref |
the client's DataSnapshot |
value is a mutable copy |
Errors are FirebaseExceptions of firebase_core with the plugin firebase_database and FlutterFire's codes:
permission-denied, disconnected, max-retries, overridden-by-set, unavailable, network-error,
index-not-defined, write-cancelled, failure, data-stale, unknown, plus the client's database-closed and too-big,
which FlutterFire does not have.
Transactions #
The handler is synchronous and may run several times: first on the value the client holds (null when it holds
none), then again on the server's value each time the server says the local one was stale. Each run gets its own
mutable copy (Map<Object?, Object?>, List<Object?>), so changing it and returning it is safe. Return
Transaction.success(value) to write or Transaction.abort() to stop: an abort completes with committed: false
and the snapshot the handler saw, and writes nothing. An exception the handler throws fails runTransaction with
that same object. With applyLocally: false, listeners see no local event before the server's answer.
Not supported #
- Web: use the official plugin.
- Long polling: the client speaks only the WebSocket protocol.
DatabasePlatform.getChannelArgumentsis not overridden and throwsUnimplementedError, as in FlutterFire's web implementation: only the method-channel implementation builds channel arguments, and the officialfirebase_databasenever calls it.
How it differs from the official plugin #
- Streams are not broadcast streams. Each listen of
onValue(or another event stream) is its own registration with its own initial event, and cancelling it removes only that registration, as the client's streams do. - Settings are captured when the client is created, at the first I/O of an instance (a read, a write, the first
listen of an event stream,
push,goOnline...).push()makes no request, but it needs the client for the server time offset of the key, so calluseDatabaseEmulatorbefore it. After the client is created,useDatabaseEmulatorwith the same host does nothing and with another one throws aStateError(ignoring it would send the traffic to production);setPersistenceEnabledthat does not match the client (on disk only withpersistenceFor) andsetPersistenceCacheSizeByteswith another size than the stored one are ignored with a debug message, as on Android;setLoggingEnabledtakes effect at once. - An app deleted and created again under the same name with other options gets no client while an instance of
the earlier app is kept: each call that needs one throws a
StateError(see "Emulator, logging, deleting an app"). The instances and their settings stay with the earlier app, are not replaced and are never released, so the name throws for the rest of the process unless it is created again with the earlier options; use another app name or restart. - Persistence is injected as a factory from the cache size (
persistenceFor), not switched on by the native SDK. - References are made without the client:
ref,child,parent,root,key,pathandonDisconnect()only build a location. So an invalid key,ref('a.b'), is reported at the first read or write of it (anArgumentError), not when the reference is made. - Values are mutable copies, typed
Map<Object?, Object?>andList<Object?>as the native plugins type them, made once per snapshot; two listeners never share one.snapshot.child(...)andsnapshot.childrenmake their own copies from the client's data, andchildrenmakes new ones on each iteration: a change to the parent'svaluemap, or to the value of a child taken from an earlier iteration, is not seen through them. The native plugins derive children from the parent'svaluemap, so there they see it. - A stream ends after the server cancels its listen (
permission-denied, then done), as the client's streams do; on Android the stream reports the error and stays open, so anawait forloop ends here and waits there. - Closing the client or deleting the app is not an error: every stream ends with done and no error event, as the
client's streams do. FlutterFire's
once(eventType)is thefirstof the stream of that event type (onValueby default), so aonce()pending then fails withStateError"No element" instead ofdatabase-closed;get()and the other Futures fail withdatabase-closed. - A child snapshot keeps its priority (
snapshot.child('a').priority,children); the native plugins give null. - A whole-number priority is an
int(1, not1.0), as values are; on Android FlutterFire returns every numeric priority as adouble, sopriority as doublemust become(priority as num).toDouble(). - The snapshot of a child event has the child's reference (
event.snapshot.refofonChildAddedis the child, as in the JS and native SDKs); FlutterFire gives the query's reference. Code that usedevent.snapshot.refas the list's reference must use the query's reference instead. - A null cursor stays null:
startAt(null)is a bound at null (which sorts first), as in JS; on Android FlutterFire sends the string"null". pathhas a leading slash ('/notes/a','/'at the root), as on the web; native has none.- Modifiers are applied in one order (orderBy, start, end, limit) whatever the call order, so
startAt(1).orderByChild('x')works; an invalid combination is an error of the stream or ofget, not of the call. - Error codes: the client's
write-canceledis reported aswrite-cancelledandoperation-failedasfailure, FlutterFire's spellings;database-closed(a Future of a closed client or deleted app) is not in FlutterFire. - Programmer errors stay Dart errors: a bad path, key, value or query is an
ArgumentErroror aStateError, and a transaction handler's exception is rethrown as itself, where the native plugins reportFirebaseException('unknown'). keepSyncedis a flag per query, not a counter.- On Windows,
ServerValue.increment,startAfterandendBeforework, and a rejected transaction ispermission-denied; the native C++ plugin gets these wrong (FlutterFire's own tests skip them there).
Platforms #
| Platform | Selected by | Notes |
|---|---|---|
| Linux | automatic (dartPluginClass) |
firebase_core is installed too, unless another package did it first. FlutterFire has no Linux plugin for firebase_database. Run on Linux x64 (Debian 13 container): the example app builds and passes its 4 integration tests under a virtual display |
| Windows | automatic when a direct dependency (dartPluginClass) |
replaces FlutterFire's native plugin (C++ SDK); firebase_core stays native. Run on Windows 11 x64, Flutter 3.47.8, debug: the example app passes its 4 integration tests against the emulator |
| macOS, Android, iOS | FirebaseDatabaseDart.use() |
firebase_core and the native SDK stay in the build |
| Web | not supported | use the official plugin |
Under flutter test, the registration on Linux leaves firebase_core alone, so that setupFirebaseCoreMocks keeps
working; call use() to choose otherwise.
Example #
example/main.dart writes, listens, runs a transaction and registers an onDisconnect write
against the Database emulator.