2

I have multiple collections in flutter firebase. For example, collection(A), collection(B), I want to show the data in these multiple collections as a single list on my homepage. Is there a way to this?

Thanks in advance

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can you be more explicit about why the data is in separate collections? E.g. if this is a join-like scenario where you have a collection of chat messages, and need to look up the name of each user in a users collection, then the common approach are listed here: https://stackoverflow.com/questions/59061225/how-do-i-join-data-from-two-firestore-collections-in-flutter – Frank van Puffelen Jan 21 '22 at 21:48
  • @FrankvanPuffelen Hello, it's not a chat app. In fact, at first, I did not anticipate that I would encounter such a problem while creating the base. Do you think it would be easier for me to rearrange the database, or would it be easier for me to show multiple collections in one place? – Yunus Dikenlitarla Jan 22 '22 at 05:57

2 Answers2

0

There's no way combining Firestore queries from multiple collections into one query. If you really want to keep the data in two different collections, you should create two separate queries. After creating, you have to merge two separate queries. You can find more information about this here.

It would be easier for you to change your Firestore Data Structure and store the two collection into one collection instead. After changing your Firestore Data Structure, you can then query like e.g. below:

FirebaseFirestore.instance
    .collection('products')
    .where('productType', whereIn: <String>['product1', 'product2']);

You could also check Firestore Data Stucture guide here.

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19
0

I don't think so, since each stream builder will subscribe to one collection to show its documents. Maybe it's better to make Lists on the same screen separated by Text or any sized box widget.