简体   繁体   中英

Firestore doesn't listen for query (Flutter)

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM