firedart 0.6.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 87

firedart #

pub package

A dart-native implementation of the Firebase Auth and Firestore SDKs.

This library attempts to minimize dependencies with the intention of making it able to run in any environment capable of executing dart code. Currently it has been successfully tested using the dart runtime (x86-64 and arm32) as well as on Flutter Android, iOS and Desktop.

Currently the only supported services are Firebase Auth and Firestore.

Dependencies #

Add firedart to your pubspec.yaml file:

  firedart: [latest version]

Firebase Auth #

The FirebaseAuth class implements the necessary functionality for managing accounts. It currently only supports Email/Password sign-in, so make sure it's enabled under Authentication -> Sign-in Method.

You'll also need to go to your Firebase Console, open Project Settings and under the General tab copy the Web API Key.

Note: in order to reduce external dependencies this library doesn't include a mechanism for persisting tokens. Please look at the following examples based on SharedPreferences and Hive.

Usage #

import 'package:firedart/firedart.dart';

FirebaseAuth has a singleton version which should be enough for most use cases. You'll need to initialise it with your API key and a token store (see note above):

FirebaseAuth.initialize(apiKey, await HiveStore());
await FirebaseAuth.instance.signIn(email, password);
var user = await FirebaseAuth.instance.getUser();

Alternatively you can instantiate your own FirebaseAuth object:

var firebaseAuth = FirebaseAuth.(apiKey, await PreferencesStore());
await firebaseAuth.signIn(email, password);
var user = await firebaseAuth.getUser();

Further usage examples can be found in the integration tests.

Limitations #

  • Currently the only supported authentication provider is Email/Password.

Firestore #

The Firestore class is a basic implementation of the service's RPC interface. The API is similar (but not identical) to that of the official SDK.

Usage #

import 'package:firedart/firedart.dart';

As with FirebaseAuth, Firestore offers a singleton version that needs to be initialised with your Project ID, which you can find under Project Settings -> General:

var map = await Firestore.instance.collection("users").get();
var users = UserCollection.fromMap(map);

You can also instantiate your own Firestore object. Please note that if your database requires authenticated access, you'll need to pass along an instance of FirebaseAuth.

var firebaseAuth = FirebaseAuth.(apiKey, await HiveStore());
var firestore = Firestore(projectId, auth: firebaseAuth);

await firebaseAuth.signIn(email, password);
var map = await firestore.collection("users").get();
var users = UserCollection.fromMap(map);

Further usage examples can be found in the integration tests.

Limitations #

  • Collection queries (limit, sort, etc.) are currently not supported.
  • The data is not cached locally.
  • Failed writes (e.g. due to network errors) are not retried.
  • Closed streams are not automatically recovered.

Regenerating the RPC stubs #

The Firestore RPC stubs are based on Google's official protobuf definition files from googleapis.

To regenerate them, you will need to check out both googleapis and protobuf.

Set the PROTOBUF and GOOGLEAPIS environment variables to point to your clones of the above repositories respectively, and then run:

$ tool/

Debugging #

For debugging Firebase Auth you can use VerboseClient, an HTTP client that logs all communication to the console. The logs can expose sensitive data including passwords and keys, so it's recommended to only enable it for development builds. In Flutter this can be achieved using the kReleaseMode constant from the foundation package:

var client = !kReleaseMode ? VerboseClient() : http.Client();
var firebaseAuth = FirebaseAuth(apiKey, await PreferencesStore(), httpClient: client);

Securing Tokens #

If you're running your code in an environment that requires securing access tokens, you can extend TokenStore to persist data in a secure maner, e.g. by encrypting the data or storing it in an external vault. Example implementations can be found in token_store.dart.

[0.6.5] - 4 January 2020.

  • Return user on signup and signin
  • Expose Firebase user id
  • Fix code warnings

[0.6.4] - 13 December 2019.

  • Update to latest Firebase auth endpoints
  • Remove dart:io dependency
  • Throw exception on auth errors

[0.6.3] - 20 November 2019.

  • Fix linter warnings

[0.6.2] - 20 November 2019.

  • Update Protobug and GRPC dependencies to latest versions

[0.6.1] - 21 July 2019.

  • Add generic Reference factory

[0.6.0] - 21 July 2019.

  • Implement collection streaming
  • Deprecate some getter methods in favour of dart's getter syntax

[0.5.1] - 17 July 2019.

  • Improve token storage

[0.5.0+1] - 16 July 2019.

  • Cleanup to satisfy dart pub analysis

[0.5.0] - 16 July 2019.

  • Refactor the code to use RPC instead of REST for Firestore.
  • Implement listening to document real-time updates.

[0.1.0] - 26 June 2019.

  • Fix issues identified in the dart pub analysis.

[0.0.1] - 26 June 2019.

  • Initial release.


import 'dart:io';

import 'package:firedart/firedart.dart';

const apiKey = 'Project Settings -> General -> Web API Key';
const projectId = 'Project Settings -> General -> Project ID';
const email = '';
const password = '1234';

Future main() async {
  FirebaseAuth.initialize(apiKey, VolatileStore());
  Firestore.initialize(projectId); // Firestore reuses the auth client

  var auth = FirebaseAuth.instance;
  // Monitor sign-in state
  auth.signInState.listen((state) => print("Signed ${state ? "in" : "out"}"));

  // Sign in with user credentials
  await auth.signIn(email, password);

  // Get user object
  var user = await auth.getUser();

  // Instantiate a reference to a document - this happens offline
  var ref = Firestore.instance.collection('test').document('doc');

  // Subscribe to changes to that document => print('updated: $document'));

  // Update the document
  await ref.update({'value': 'test'});

  // Get a snapshot of the document
  var document = await ref.get();
  print('snapshot: ${document['value']}');


  // Allow some time to get the signed out event
  await Future.delayed(Duration(seconds: 1));


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  firedart: ^0.6.5

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:firedart/firedart.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
fixnum ^0.10.11 0.10.11
grpc ^2.1.3 2.1.3
http ^0.12.0+3 0.12.0+4
protobuf ^1.0.1 1.0.1
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
googleapis_auth 0.2.11+1
http2 1.0.0
http_parser 3.1.3
meta 1.1.8
path 1.6.4
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
pedantic ^1.9.0 1.9.0
test ^1.6.4