A Flutter plugin to use the Cloud Firestore API.
For Flutter plugins for other Firebase products, see FlutterFire.md.
Note: This plugin is still under development, and some APIs might not be available yet. Feedback and Pull Requests are most welcome!
Recent versions (0.3.x and 0.4.x) of this plugin require extensible codec functionality that is not yet released to the beta channel of Flutter. If you're encountering issues using those versions, consider switching to the dev channel.
To use this plugin:
android/build.gradle
file contains the maven.google.com
as described here.cloud_firestore
as a dependency in your pubspec.yaml file.import 'package:cloud_firestore/cloud_firestore.dart';
Adding a new DocumentReference
:
Firestore.instance.collection('books').document() .setData({ 'title': 'title', 'author': 'author' });
Binding a CollectionReference
to a ListView
:
class BookList extends StatelessWidget { @override Widget build(BuildContext context) { return StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection('books').snapshots(), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (snapshot.hasError) return new Text('Error: ${snapshot.error}'); switch (snapshot.connectionState) { case ConnectionState.waiting: return new Text('Loading...'); default: return new ListView( children: snapshot.data.documents.map((DocumentSnapshot document) { return new ListTile( title: new Text(document['title']), subtitle: new Text(document['author']), ); }).toList(), ); } }, ); } }
Performing a query:
Firestore.instance .collection('talks') .where("topic", isEqualTo: "flutter") .snapshots() .listen((data) => data.documents.forEach((doc) => print(doc["title"])));
Running a transaction:
final DocumentReference postRef = Firestore.instance.document('posts/123'); Firestore.instance.runTransaction((Transaction tx) async { DocumentSnapshot postSnapshot = await tx.get(postRef); if (postSnapshot.exists) { await tx.update(postRef, <String, dynamic>{'likesCount': postSnapshot.data['likesCount'] + 1}); } });
See the example
directory for a complete sample app using Cloud Firestore.