Get firestore collections based on values in array list in flutter

Viewed 3283

I'm trying to get firestore collections based on an array list.

I need to get collection 'items' based on the array 'favourites' inside collection 'users'.

Below are the two images of my database.

https://i.stack.imgur.com/Vmeom.png enter link description here

1 Answers

You can do the following:

  onPressed(){
    Firestore.instance.collection("users").getDocuments().then((querySnapshot){
      querySnapshot.documents.forEach((element){
        List value = element.data["favourites"];
        Firestore.instance.collection("items").document(value[0]).get().then((value){
          print(value.data);
        });
      });
    });
  }

Query the collection users -> get the array favourites -> use the value inside the array in the document() method.

Related