firebase_flutter_dart 0.1.0
firebase_flutter_dart: ^0.1.0 copied to clipboard
Unofficial. Sets up the pure-Dart Firebase clients in a Flutter app in one call: app identity, Auth with the platform's secure store, SQLite caches for Firestore and the Realtime Database, and the app [...]
firebase_flutter_dart #
Unofficial. Not affiliated with Google or Firebase.
Sets up the pure-Dart Firebase clients of this repository in a Flutter app in one call. It is
for apps that use the Dart APIs directly (FirebaseFirestore.instanceFor, FirebaseUserAuth,
FirebaseDatabase). An app on the FlutterFire API (firebase_core, cloud_firestore, ...) adds
the *_dart_platform packages instead.
Status: 0.1.0, early. Analyzed; its unit tests cover
FlutterPlatformDependenciesonly. Breaking changes are expected.
Setup #
dependencies:
firebase_flutter_dart:
git:
url: https://github.com/long1eu/firebase_dart_sdk
path: firebase_core/firebase_flutter_dart
import 'package:firebase_flutter_dart/firebase_flutter_dart.dart';
import 'package:firebase_flutter_dart/firestore.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
// 1. The options of your app (Firebase console, Project settings, Your apps).
const FirebaseOptions options = FirebaseOptions(
apiKey: '<YOUR_API_KEY>',
appId: '1:1234567890:web:0123456789abcdef',
projectId: 'my-project',
);
// 2. The app, its Auth, and the cache directory.
final FirebaseFlutter firebase = await FirebaseFlutter.initialize(options);
// 3. The instances you use.
final FirebaseFirestore firestore = firebase.firestore();
runApp(MyApp(firestore: firestore, auth: firebase.auth));
}
The one dependency brings the clients: package:firebase_flutter_dart/firestore.dart exports
cloud_firestore_dart and package:firebase_flutter_dart/database.dart exports
firebase_database_dart; the main library exports firebase_app_dart and
firebase_auth_providers_dart. The products have a library each because their names overlap
(Query, Transaction, ...).
What FirebaseFlutter.initialize does #
| Step | What you wrote before |
|---|---|
Fills the options with the running app's package and signing SHA-1 (Android) or bundle ID (iOS, macOS), for an API key limited to the app (firebase_app_identity) |
AppIdentity.apply(options) |
Creates the FirebaseApp with a FlutterPlatformDependencies |
a PlatformDependencies subclass, rxdart, connectivity_plus |
Creates FirebaseUserAuth for the app, the token provider of Firestore and the Database, with the session in the platform's secure store (firebase_auth_secure_storage_flutter), and restores it |
FirebaseUserAuth.forApp(app, storage: SecureAuthStorage()) |
| Finds the application support directory for the caches | path_provider |
Before it chooses, initialize probes the secure store: it writes, reads back and deletes a test
entry. A read alone cannot tell, because a macOS build without the keychain-access-groups
entitlement reads a missing key as null and fails only at a write. When a step fails because the
store cannot be used in this run (no Secret Service on Linux, no Keychain entitlement on macOS, no
interactive logon on Windows), the session is kept in memory only: the user signs in again at
the next start. It is never written to a plain file. The reason is printed once with
debugPrint. Any other failure (a locked iOS device, for example) keeps the secure store, and the
app starts signed out when the session cannot be read.
If a step fails, initialize deletes the app it created before it rethrows, so it can be called
again.
Then:
firebase.firestore({databaseId, settings}):FirebaseFirestore.instanceForwith the cache in a SQLite file in<application support>/firestore(cloud_firestore_dart_sqlite). PassSettings(cacheSettings: MemoryCacheSettings())for a memory cache,Settings().withEmulator(host, port)for the emulator.firebase.database({databaseUrl, emulatorHost, emulatorPort}): a newFirebaseDatabasewith the cache in<application support>/firebase_database(firebase_database_dart_sqlite). Call it once per database and keep the instance: a second client of the same database waits for the first one's cache file, then runs memory only.emulatorHostoremulatorPortselects the emulator (localhostand 9000 for the one not given).firebase.auth: theFirebaseUserAuth.initialize(authEmulatorHost: '127.0.0.1:9099')sends it to the Auth emulator.- When a desktop app is asked to quit, the Firestore instances are terminated and the databases
closed, so the SQLite files are closed cleanly.
firebase.close()does the same earlier.
On the web there is no cache directory: both caches are in memory.
FlutterPlatformDependencies #
The PlatformDependencies of a Flutter app. initialize creates one; create your own to pass
connectivity events:
final FirebaseFlutter firebase = await FirebaseFlutter.initialize(
options,
dependencies: FlutterPlatformDependencies(
connectivity: Connectivity().onConnectivityChanged.map(
(List<ConnectivityResult> r) => r.any((ConnectivityResult c) => c != ConnectivityResult.none),
),
),
);
onBackgroundChanged:trueon pause,falseon resume.onNetworkConnected: the events ofconnectivity. Without it nothing is emitted and Firestore keeps its own detector, which watches the addresses of the network interfaces while a stream is failing. No connectivity plugin is a dependency of this package:connectivity_plususesnmanddbuson Linux, which are under the Mozilla Public License 2.0.onExit(action): runsactionwhen a desktop app is asked to quit.
Platform setup #
Still by hand, as in the Flutter guide:
- macOS:
com.apple.security.network.clientin both entitlements files; for the secure store,keychain-access-groupsand a signed build (seefirebase_auth_secure_storage_flutter). - Android: the
INTERNETpermission for release builds. - Linux:
libsecret-1-devto build.