firebasefluttergoogle-cloud-firestorequeryingstream-builder

FLUTTER, FIRESTORE : I have a streambuilder that returns a certain field from all the documents in the collection..how do I add query?


I have a collection called todo and each document in todo contains the fields : task, boolean isDone, user_id, and task_id. Each user in my app has an ID so I only want to retrieve the tasks for their id. I have the code to retrieve the task field from all the documents but I want to query the collection so that I only retrieve the tasks for where the user_id=docid..docid is the variable i know.

body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection('todo').snapshots(),
          builder: (context, snapshot) {
            return ListView.builder(
                itemCount: snapshot.data.docs.length,
                itemBuilder: (context, index) {
                  DocumentSnapshot course = snapshot.data.docs[index];
                  return ListTile(title: Text(course['task']));
                });
          },
        )

So I need help adding a query to this. I don't think i can use index anymore, though i am unsure . I am new to Flutter and Firestore so all the Streambuiler and Futurebuilder things haven't clicked yet. Please lmk if there any easy to understand references out there also.


Solution

  • You can add a query like so.

    stream: FirebaseFirestore.instance
                .collection('todo')
                .where('user_id', isEqualTo: docid)
                .snapshots(),