firedart 0.6.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

firedart #

pub package

An incomplete, dart-native implementation of the Firebase SDK based on Firebase's REST and RPC interfaces.

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 ├╣sing 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:

dependencies:
  firedart: ^0.6.1

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: if you're targeting Flutter Mobile you might want to import PreferencesStore into your project.

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 PreferencesStore());
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:

Firestore.initialize(projectId);
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 PreferencesStore());
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/regenerate.sh

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);
var firestore = Firestore(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.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.

example/main.dart

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 = "you@server.com";
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();
  print(user);

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

  // Subscribe to changes to that document
  ref.subscribe().listen((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"]}");

  auth.signOut();

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

  exit(0);
}

Use this package as a library

1. Depend on it

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


dependencies:
  firedart: ^0.6.1

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Nov 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:firedart/firedart.dart with components: io.

Maintenance issues and suggestions

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (grpc, protobuf).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
fixnum ^0.10.9 0.10.11
grpc ^1.0.3 1.0.3 2.1.3
http ^0.12.0+2 0.12.0+2
protobuf ^0.13.15 0.13.16+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.3
googleapis_auth 0.2.10
http2 1.0.0
http_parser 3.1.3
meta 1.1.8
path 1.6.4
pedantic 1.8.0+1
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.6.4