Crypto Miner
Crypto Miner

Reputation: 5

Flutter / Firestore - How can i fetch collection in subcollection userid equal to currentuser

My Firestore structure here,

my firestore structure

i want to fetch lesson data.But only users with equal user ids in the member collection.But i can't. is that worst usage?

  body: StreamBuilder(
    stream: firestore
        .collection('lessons')
        .where('members', isEqualTo: FirebaseAuth.instance.currentUser.uid)
        .snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (!snapshot.hasData) {
        return Center(
          child: CircularProgressIndicator(),
        );
      } else {
        return Container(
          child: ListView(
            scrollDirection: Axis.vertical,
            children: snapshot.data.docs.map((document) {
              return Card(
                margin: EdgeInsets.all(5),
                child: Column(
                  children: [
                    ListTile(
                      title: Text(document['lessonTitle']),
                      leading: Icon(Icons.label),
                      trailing: Text(document['lessonSubtitle']),
                    )
                  ],
                ),
              );
            }).toList(),
          ),
        );
      }
    },
  ),

Upvotes: 0

Views: 162

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

There is no way to select documents from one collection based on values in another (sub)collection. Queries in Firestore can only consider information in the documents that they're returning.

If you want to select lessons based on their members, you'll need to include that information in each lesson document itself. The common way to do this is to add a members array to each lesson document, use arrayUnion and arrayRemove operations to manipulate that array, and then use array-contains to query the array.

Upvotes: 1

Related Questions