A Flutter Package to use the Cloud Firestore API for cross platform Flutter Apps.
To use this package:
-
Using the Firebase Console, add a web app to your project. Note Your app may be intended to run on IOS, Android, MacOS, Linux , Web or Windows platforms (or all of them) - you still add your app as a web app in firebase
-
Go to Project Settings , copy down
- Project ID
- Web API Key
-
Add
cloud_firestore_rest
andglobal_configuration
as a dependency in yourpubspec.yaml
file. -
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.
You can register your users in firebase and use googleapis identitytoolkit to login and logout users.
try {
Map<String, dynamic> response = await Firestore.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
import 'package:cloud_firestore_rest/cloud_firestore_rest.dart';
Future<List<Item>> Firestore.get({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 Firestore.get(query: [
Query(field: 'orderDate', op: FieldOp.GREATER, value: searchDate),
Query(field: 'customerId', value: searchId),
]);
} catch(error) {
//handle error
Call Firestore.get(collection: 'collectionId')
without supplying a query
argument to get all the documents from the collection.
...
Map<String, dynamic> document = await Firestore.getDocument(
collection: 'items', id: searchId,
); // returns null if not found
Item item = Item.fromJson(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
}
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
}
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
}