如何使用Flutter监听Cloud Firestore中的文档更改?

如何使用Flutter监听Cloud Firestore中的文档更改?,第1张

如何使用Flutter监听Cloud Firestore中的文档更改

通过阅读

cloud_firestore
文档,您可以看到可以从获取
Stream
来自的。
Query``snapshots()

为了让您理解,我将稍微转换一下代码:

CollectionReference reference = Firestore.instance.collection('planets');reference.snapshots().listen((querySnapshot) {  querySnapshot.documentChanges.forEach((change) {    // Do something with change  });});

您也不应在事务中运行它。该 颤振的方式 这样做的使用

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(),    );  },);

如果您想了解更多信息,可以查看源文件,该文件已被很好地记录下来,而不是不言自明的。

另请注意,我已更改

docChanges
documentChanges
。您可以在
query_snapshot
文件中看到它。如果您使用的是IntelliJ或Android
Studio之类的IDE,也可以非常轻松地单击其中的文件。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5051888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存