0

I have a Collection called Channels and a sub collection called messages , I want to get the data of every Channels and also the data of sub collection messages:

 StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('Channels')
            .where('particpants', arrayContains: UserInfoData.userID)
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
            final channesl = snapshot.data;
          return StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection('Channels')
                .doc(channels[i][channelsID])  >>>> **here I want to get the messages belong to each channels**
                .collection('messages')
                .snapshots(),
            builder:
                (BuildContext context, AsyncSnapshot<dynamic> snapshot) {},
          );
        },
      )

how I can get the channelsID of every channels ?

I wish my question is clear

enter image description here

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Not an exact duplicate, but do check out https://stackoverflow.com/questions/59061225/how-do-i-join-data-from-two-firestore-collections-in-flutter – Frank van Puffelen Oct 23 '21 at 22:55

1 Answers1

0

What you're trying to do is not possible in a single query.

Your best option is to make one query for Channels, then another collection group query for messages. The collection group query will give you all documents in all subcollections with the same name. You will need to manually merge each document from messages with the Channel it came from by looking at the path of the document (or adding more data to each message to indicate which Channel it's related to.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441