0

I'm trying to perform a compound query in Cloud Firestore with the isEqualTo operator in Flutter, inside a StreamBuilder, like this:

Firestore.instance
  .collection('produtos')
  .where("NProduto", isEqualTo: productClicked)
  .snapshots()
  .listen((data) =>
  data.documents.forEach((doc) => nomeProdutoClicked = doc["Nome"]));

But when i try to use forEach in the last line, it never seems to add to a list, to set to a variable or anything like that, the only thing that worked was printing (doc). I've tried doing it outside the StreamBuilder in a separate methos but I wasn't able to make it work in any way. Essentially, what I'm trying to do is get a document from a different collection as that as the one I'm using in the StreamBuilder.

If you know another way to do this, or know how to solve this issue, please help me. I will be eternally grateful hahhahah :)

1 Answers1

0

Basically you can add

return new ListView(children: getItems(snapshot));

And then, define getItems

getItems(AsyncSnapshot<QuerySnapshot> snapshot) {
    return snapshot.data.documents
        .map((doc) => new ListTile(list:list)
        .toList();
  }

It should looks like:

return new StreamBuilder<QuerySnapshot>(
        stream: 
        Firestore.instance.
        collection('produtos').
        .where("Produto", isEqualTo: productClicked)
        .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (!snapshot.hasData) return new Text("There is no products");
          return new ListView(children: getProductsItems(snapshot));
        });


  getProductsItems(AsyncSnapshot<QuerySnapshot> snapshot) {
    return snapshot.data.documents
        .map((doc) => new ListX(title: new Text(doc["Nome"])
        .toList();
  }
Joss Baron
  • 1,441
  • 10
  • 13
  • I think this might work, but I'm not sure about what you meant by the `ListX` and `.toList()` part. Could you please explain? Edit: I'm trying to get the `isEqualTo` from a list, so I have to loop through it, that's why it's not really working. – Juliano Portela May 20 '20 at 14:50