'How to merge two firestore queries in the Flutter project and How do I remove the duplicate documents from these two streams?

I needed to combine two firestore query stream in my flutter project. How do I do this? I tried StreamZip([Stream1, stream2]) method to combine the streams and it worked for me. but the streams maybe contains the same documents. so when I listed them all of the documents are listed, even there is a duplicate of it. How do I remove the duplicate documents from these two streams?

   Stream<List<QuerySnapshot>> getData() {
    Stream defaultStream1 = _firestore
        .collection("Gyms")
        .where("gymPlaceTags", arrayContainsAny: ["dubai"])
        .orderBy('createdAt', descending: true)
        .snapshots();
    Stream defaultStream2 = _firestore
        .collection("Gyms")
        .where("gymFavTags", arrayContainsAny: ["ajman"])
        .orderBy('createdAt', descending: true)
        .snapshots();
    return StreamZip([defaultStream1, defaultStream2]);
  }


Solution 1:[1]

I think it's better to use serial where clauses.

Stream stream = FirebaseFirestore.instance.collection("Gyms").where('gymPlaceTags',  arrayContainsAny: ["dubai"]).where("gymFavTags", arrayContainsAny: ["ajman"]).orderBy('createdAt', descending: true).snapshots();

This type of query has worked for me. Hope this will solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Melanga Dissanayake