Cloud Firestore Package for Flutter - Work in Progress

Issues

TODO serialization rom dart to firestore - for DateTime, List and Map types

A Flutter Package to use the Cloud Firestore API by cross platform Flutter Apps.

Setup

To use this package:

  1. Using the Firebase Console, add a web app to your project.

  2. Go to Project Settings , copy down

    1. Project ID
    2. Web API Key
  3. Add cloud_firestore_rest and global_configuration as a dependency in your pubspec.yaml file.

  4. Edit lib/main.dart and edit code

    +import 'package:global_configuration/global_configuration.dart';
    ...
    
    void main()  {
     + GlobalConfiguration().loadFromMap({
     +  'projectId': '<project ID>',
     +  'webKey': 'web API key',
     +});
      runApp(MyApp());
    }
    ...
    
    

Of course you may use any of the GlobalConfiguration load methods of your choice to configure your app. The package expects projectId and webKey to be available as part of global configuration.

Usage

Authentication

You can register your users in firebase and use googleapis identitytoolkit to login and logout users.

try {
Map<String, dynamic> response = await signInOrSignU[(
  email: 'abcd@efg.com',
  password: '123456',
  action: AuthAction.signUp **OR** AuthAction.signInWithPassword,
);
} catch (error) {
  // handle error
}

The returned response contains

  • firebase userId
  • auth token
  • login expiry

Read from firestore

Performing a query

...

import 'package:cloud_firestore_rest/cloud_firestore_rest.dart';

Future<List<Item>> getItems({List<Query> query)}) {
  List<Item> items;
  final documents = await Firestore.get(
    collection: 'items',
    query: query,
    );
  documents.map((doc) => _items.add(Item.fromJson(doc)));
  return items;
}

...
try {
List<Item> items = await getItems(query: [
  Query(field: 'orderDate', op: FieldOp.GREATER, value: searchDate),
  Query(field: 'customerId', value: searchId),
]);
} catch(error) {
  //handle error

Get all documents from a collection

Call Firestore.get(collection: 'collectionId') without supplying a query argument to get all the documents from the collection.

Get a specific document

...
Map<String, dynamic> document = await Firestore.getDocument(
  collection: 'items', id: searchId,
  ); // returns null if not found
Item item = Item.fromJson(document);
...


Write to firestore

Add new Collection/document

Creates a new collection if collection does not exist. Adds the document if the document does not exist. Returns created document.


try {
  final document = await Firestore.add(
    collection: 'orders',
    body: order.toJson
    );
  order.id = docurment['id'];
} catch (error) {
  // handle error
}

Update document

Update a document, add if the document does not exist

Updates only the fields passed via the body argument. The fields can be new - and need not be part of the existing document.

If document is not found and the optional argument addNew is true, adds the document to the collection.

If id is not supplied, firestore creates one for the document.

Note: If an entire document is not passed to this function, the API will not throw error, instead will write a truncated document.

...

try {
  await Firstore.setAll(
    collection: 'orders',
    id: order.id,
    body: order.toJson,
    addNew: true,
  );
} catch(error) {
  // handle error
}

Delete document

Deletes the document in the collection specified.

Throws error if document does not exist.

...

try {
  await Firstore.delete(
    collection: 'orders',
    id: order.id,
  );
} catch(error) {
  // handle error
}

Libraries

cloud_firestore_rest