0

I have two different collections in firestore of similar things in flutter application. I want to combine them in one Query. How can I combine them as a one Query or Stream?

I am new to firestore and learning about this.

Query query1 = Firestore.instance.collection("products1");

Query query2 = Firestore.instance.collection("products2");

Query MasterQuery;

    StreamBuilder(
        stream: MasterQuery.snapshots(),
        builder: (context, snapshot) {},
                );builder: (context, snapshot) {
      return !snapshot.hasData
          ? Text('PLease Wait')
          : ListView.builder(
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                DocumentSnapshot products =
                    snapshot.data.documents[index];
                return ProductItem(
                  name: products['name'],
                  imageUrl: products['imageURL'],
                  price: products['price'],
                  discription: products['description'],
                );
              },
            );
    },

How should I do it?

  • Does this answer your question? [Flutter merge two firestore streams into a single stream](https://stackoverflow.com/questions/53287717/flutter-merge-two-firestore-streams-into-a-single-stream) – dshukertjr Jan 09 '21 at 10:01
  • You can look at this solution https://stackoverflow.com/questions/53287717/flutter-merge-two-firestore-streams-into-a-single-stream – dshukertjr Jan 09 '21 at 10:02
  • @dshukertjr Hi Thanks for the solution , but What I need is to combine the Two `Query` instead of combining Two `Stream`, because then we can paginate the `Query` and I don't know how the stream pagination can be done. If you know how the `Stream` pagination is done I would be glad to know about it. –  Jan 09 '21 at 10:23

1 Answers1

0

Unfortunately, there is no way of combining two firestore queries from different collections into one query. If you want to keep the data in two different collections, you would have to have two separate queries. You can combine the two queries with the instruction explained here.

Another option is to change the database structure and store the two collections into one collection instead. If you want to query them as same stream, the data model is pretty similar I assume. Maybe you can have a productType field to store the product type and create a in query like this:

FirebaseFirestore.instance
    .collection('products')
    .where('productType', whereIn: <String>['product1', 'product2']);
dshukertjr
  • 15,244
  • 11
  • 57
  • 94
  • Hi, what if its from the same collection? I have an issue where I have more than 10 elements in my list to query in whereIn. Can i query multiple whereIn query can then combine them then? – Mayb3Not Jan 10 '21 at 13:12
  • 1
    @Mayb3Not You can certainly combine multiple stream into one, or there may be a better solution. Do you have a question on stack overflow for your problem? If not, if you could create one and copy the link here, I could take a look. – dshukertjr Jan 10 '21 at 22:23
  • 1
    I combined the result of the multiple wherIn query changing them into a list and then adding all of them into a huge list. This is my solution to a social media feed sorting by chronological order – Mayb3Not Jan 11 '21 at 16:23
  • 1
    @Mayb3Not Creating social media using firestore is tough. Good work man! – dshukertjr Jan 11 '21 at 23:02