Reputation: 5768
I'm trying to listen to document changes with the following code: (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)
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!
Upvotes: 1
Views: 817
Reputation: 598765
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.
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.
Upvotes: 1