Flutter Firestore如何收听对一个文档的更改 [英] Flutter Firestore How To Listen For Changes To ONE Document
本文介绍了Flutter Firestore如何收听对一个文档的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想在一个文档被更改或更新时得到通知。获取更新的每个示例始终使用集合。我试图仅使用一个文档就实现了这一点,但是它从未得到任何更新。这是我现在无法使用的内容:
I just want to get notified whenever ONE document is changed or updated. Every example of getting updates is always using collections. I tried to implement that with only using one document and it never gets any updates. Here is what I have right now that doesn't work:
@override
Widget build(BuildContext context) {
StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance
.collection("users")
.document(widget.uid)
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
user = snapshot.data.data as User;
});
我已经调试了100次,但从未进入 builder:部分。顺便说一下,这对于文档参考不是问题。我在这里做什么错了?
I've debugged this 100 times and it never gets to the "builder:" section. And this is not a problem with the document reference by the way. What am I doing wrong here?
推荐答案
下面是一个例子
Firestore.instance
.collection('Users')
.document(widget.uid)
.snapshots()
.listen((DocumentSnapshot documentSnapshot) {
Map<String, dynamic> firestoreInfo = documentSnapshot.data;
setState(() {
money = firestoreInfo['earnings'];
});
})
.onError((e) => print(e));
但您在这里做错的是:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
user = snapshot.data.data as User;
});
用
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
var firestoreData = snapshot.data;
String info = firestoreData['info'];
});
这篇关于Flutter Firestore如何收听对一个文档的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文