Home > front end >  How can I get the document from Firebase Firestore with particular value in field?
How can I get the document from Firebase Firestore with particular value in field?

Time:09-10

I'm creating todo list in which I'll have two lists one for incomplete tasks and another for completed tasks...I'm able to retrieve the data from Firestore by pulling the whole data without any condition, but for the uncompleted list, I want to retrieve the document only with particular field values. For example, each document contains the following fields as shown in the picture:enter image description here

Here is the code which I've used to get the data from firebase firestore:

static List<Task> getTasks() {
    //convert firebase collection to list
    List<Task> tasks = [];
    FirebaseFirestore.instance.collection('Todos').snapshots().listen((data) {
      data.docs.forEach((doc) {
        tasks.add(Task.fromMap(doc.data()));
      });
    });
    return tasks;
  }

I want similar function like this to retrieve only documents with taskStatus == true. So, I can store all those tasks which have taskStatus of true on separate list and display them accordingly.

CodePudding user response:

You can use the where function. It works like below. For more: Cloud FireStore Filtering

FirebaseFirestore.instance
  .collection('Todos')
  .where('taskStatus', isEqualTo: true)
  .snapshots().listen((data) {
    data.docs.forEach((doc) {
      other_tasks.add(Task.fromMap(doc.data()));
    });
  });
  • Related