Shadi Ossaili
Shadi Ossaili

Reputation: 595

Firestore query a filed against multiple values

Is it possible to query documents fields against an array of values where it would return the fields which contain one of the array elements values

                 _fireStore
                .collection('articles')
                .orderBy('created')
                .where('projectName', isEqualTo: listHearted)
                .getDocuments()
                .asStream(),

Upvotes: 0

Views: 834

Answers (2)

Shadi Ossaili
Shadi Ossaili

Reputation: 595

i've done it using a stream within a stream.

 Stream<List<DocumentSnapshot>> streamDoc;
  StreamController<List<DocumentSnapshot>> controller =
  StreamController<List<DocumentSnapshot>>();

 void docRef() {
Firestore _fireStore = Firestore.instance;

Stream<QuerySnapshot> snapshot = _fireStore
    .collection('articles')
    .orderBy('created')
    .getDocuments()
    .asStream();

List<DocumentSnapshot> listDoc = List<DocumentSnapshot>();
snapshot.listen((snapshot) {
  snapshot.documents.forEach((e) {
    if (SharedPrefList.listHearted
            .contains(e.data['projectName'].toString()) &&
        widget.type == 'hearted') {
      listDoc.add(e);
      controller.add(listDoc);
    }
    if (SharedPrefList.listSeen
            .contains(e.data['projectName'].toString()) &&
        widget.type == 'seen') {
      listDoc.add(e);
      controller.add(listDoc);
    }
  });
});

}

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

There is currently no way to perform a query that returns items that match one of a number of values in a certain field. The workaround is to perform a query for each hearted project, and merge the results on the client.

Also see:

Upvotes: 1

Related Questions