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 FlutterPlatformDependencies only. 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.instanceFor with the cache in a SQLite file in <application support>/firestore (cloud_firestore_dart_sqlite). Pass Settings(cacheSettings: MemoryCacheSettings()) for a memory cache, Settings().withEmulator(host, port) for the emulator.
  • firebase.database({databaseUrl, emulatorHost, emulatorPort}): a new FirebaseDatabase with 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. emulatorHost or emulatorPort selects the emulator (localhost and 9000 for the one not given).
  • firebase.auth: the FirebaseUserAuth. 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: true on pause, false on resume.
  • onNetworkConnected: the events of connectivity. 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_plus uses nm and dbus on Linux, which are under the Mozilla Public License 2.0.
  • onExit(action): runs action when a desktop app is asked to quit.

Platform setup

Still by hand, as in the Flutter guide:

  • macOS: com.apple.security.network.client in both entitlements files; for the secure store, keychain-access-groups and a signed build (see firebase_auth_secure_storage_flutter).
  • Android: the INTERNET permission for release builds.
  • Linux: libsecret-1-dev to build.

Libraries

database
firebase_database_dart, for an app that depends on firebase_flutter_dart only. Get the instance with FirebaseFlutter.database.
firebase_flutter_dart
Sets up the pure-Dart Firebase clients in a Flutter app in one call.
firestore
cloud_firestore_dart, for an app that depends on firebase_flutter_dart only. Get the instance with FirebaseFlutter.firestore.