[英]Firestore doesn't listen for query (Flutter)
I'm trying to listen to document changes with the following code: (Flutter)我正在尝试使用以下代码收听文档更改:(Flutter)
Firestore.instance
.collection('myCollection')
.where("owners.${myUserID}", isEqualTo: true)
.orderBy("lastRef", descending: true)
.snapshots()
.listen((data) {
print("listening");
data.documentChanges.forEach((change) {
setState(() {
myList = data.documents;
});
});
});
This code only works once, it adds all the right documents to the list, but when a document is updated, it doens't do anything...这段代码只工作一次,它将所有正确的文档添加到列表中,但是当文档更新时,它什么都不做......
I tried this code (without the where query)我试过这段代码(没有 where 查询)
Firestore.instance
.collection('myCollection')
.orderBy("lastRef", descending: true)
.snapshots()
.listen((data) {
print("listening");
data.documentChanges.forEach((change) {
setState(() {
myList = data.documents;
});
});
});
Works perfectly, even when a document is updated即使文档已更新,也能完美运行
I thought I'd had to make an index, but i didn't see any message in the console.我以为我必须做一个索引,但我没有在控制台中看到任何消息。 Tried creating one manually.尝试手动创建一个。
Indexed fields: owners (array), lastRef (descending)
What am I doing wrong?我究竟做错了什么?
Thanks in advance!提前致谢!
That data.documentChanges.forEach((change) {
around the setState
call looks suspicious to me. As far as I can see you should set the documents to the state, whenever your listener gets called. setState
调用周围的data.documentChanges.forEach((change) {
在我看来很可疑。据我所知,只要您的侦听器被调用,您就应该将文档设置为 state。
So something like:所以像:
Firestore.instance
.collection('myCollection')
.where("owners.${myUserID}", isEqualTo: true)
.orderBy("lastRef", descending: true)
.snapshots()
.listen((data) {
setState(() {
myList = data.documents;
});
});
I doubt it'll change the problem with your query, but I'd recommend changing this either way as it simplifies your listener and makes it more idiomatic.我怀疑它会改变您的查询问题,但我建议您以任何一种方式进行更改,因为它可以简化您的侦听器并使其更加惯用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.