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 new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['title']),
subtitle: new Text(document['author']),
);
}).toList(),
);
},
);
}
}
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.
await
.snapshots
is now a method instead of a getter.setData
uses named arguments instead of SetOptions
.null
document snapshots (document not exists).DocumentSnapshot.exists
.get
to DocumentReference.DocumentReference
update and merge writesisNull
filtering in Query.where
DocumentChange.oldIndex
and DocumentChange.newIndex
to be signed
integers (iOS)example/lib/main.dart
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
Future<void> main() async {
final FirebaseApp app = await FirebaseApp.configure(
name: 'test',
options: const FirebaseOptions(
googleAppID: '1:79601577497:ios:5f2bcc6ba8cecddd',
gcmSenderID: '79601577497',
apiKey: 'AIzaSyArgmRGfB5kiQT6CunAOmKRVKEsxKmy6YI-G72PVU',
projectID: 'flutter-firestore',
),
);
final Firestore firestore = new Firestore(app: app);
runApp(new MaterialApp(
title: 'Firestore Example', home: new MyHomePage(firestore: firestore)));
}
class MessageList extends StatelessWidget {
MessageList({this.firestore});
final Firestore firestore;
@override
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: firestore.collection('messages').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int messageCount = snapshot.data.documents.length;
return new ListView.builder(
itemCount: messageCount,
itemBuilder: (_, int index) {
final DocumentSnapshot document = snapshot.data.documents[index];
return new ListTile(
title: new Text(document['message'] ?? '<No message retrieved>'),
subtitle: new Text('Message ${index + 1} of $messageCount'),
);
},
);
},
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({this.firestore});
final Firestore firestore;
CollectionReference get messages => firestore.collection('messages');
Future<Null> _addMessage() async {
final DocumentReference document = messages.document();
document.setData(<String, dynamic>{
'message': 'Hello world!',
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Firestore Example'),
),
body: new MessageList(firestore: firestore),
floatingActionButton: new FloatingActionButton(
onPressed: _addMessage,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Add this to your package's pubspec.yaml file:
dependencies:
cloud_firestore: ^0.7.4
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:cloud_firestore/cloud_firestore.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.9.0+2 | Feb 15, 2019 |
|
|
0.9.0+1 | Feb 8, 2019 |
|
|
0.9.0 | Jan 25, 2019 |
|
|
0.8.2+3 | Nov 8, 2018 |
|
|
0.8.2+2 | Nov 8, 2018 |
|
|
0.8.2+1 | Oct 18, 2018 |
|
|
0.8.1+1 | Oct 12, 2018 |
|
|
0.8.1 | Oct 2, 2018 |
|
|
0.8.0 | Sep 5, 2018 |
|
|
0.7.4 | Jul 23, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
100
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
100
|
We analyzed this package on Feb 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0-dev.28.0 <3.0.0 | ||
collection | ^1.14.3 | 1.14.11 | |
firebase_core | ^0.2.2 | 0.2.5+1 | 0.3.0 |
flutter | 0.0.0 | ||
meta | ^1.0.5 | 1.1.6 | 1.1.7 |
Transitive dependencies | |||
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |