Pat*_*cow 4 dart flutter google-cloud-firestore
我想有一个监听器方法,如果发生更改,将检查文档集合的更改.
就像是:
import 'package:cloud_firestore/cloud_firestore.dart';
Future<Null> checkFocChanges() async {
Firestore.instance.runTransaction((Transaction tx) async {
CollectionReference reference = Firestore.instance.collection('planets');
reference.onSnapshot.listen((querySnapshot) {
querySnapshot.docChanges.forEach((change) {
// Do something with change
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
这里的错误onSnapshot是没有定义的CollectionReference.
有任何想法吗?
cre*_*not 22
通过阅读cloud_firestore文档,您可以看到Stream来自a 的文件可以Query通过snapshots().
为了让您理解,我将稍微改造您的代码:
CollectionReference reference = Firestore.instance.collection('planets');
reference.snapshots().listen((querySnapshot) {
querySnapshot.documentChanges.forEach((change) {
// Do something with change
});
});
Run Code Online (Sandbox Code Playgroud)
您也不应该在事务中运行它.该颤振的方式这样做的使用StreamBuilder,直接从cloud_firestore 飞镖酒吧页面:
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(),
);
},
);
Run Code Online (Sandbox Code Playgroud)
如果您想了解更多信息,可以查看源代码,它有详细记录,不能自我解释.
还需要注意的是我改变docChanges对documentChanges.你可以在query_snapshot文件中看到.如果您使用的是IntelliJ或Android Studio等IDE,则单击其中的文件也非常容易.
| 归档时间: |
|
| 查看次数: |
10238 次 |
| 最近记录: |