Fahmi Sawalha
Fahmi Sawalha

Reputation: 622

getting all documents with the same field value firestore flutter

long story short that I have a collection called products , in this collection every product have a document that is called category , I want to get all the products with the same category and this is the structure for it . I know that I should use where().equalto , but I did not get it , any help ? enter image description here

Upvotes: 0

Views: 1381

Answers (1)

Priyesh
Priyesh

Reputation: 1313

Future<List<ProductModel>> getProductCategory(String category) async {
    QuerySnapshot query = await _firestore.collection(collection)
        .where("category", isEqualTo: category)
        .get();
    
    if(query.docs.isNotEmpty){
      query.docs.forEach((element) {
        productCategory.add(ProductModel.fromSnapshot(element.data()));
      });
    }
      return productCategory;
  }

This will return all documents which having category is equal to "eye".

Upvotes: 2

Related Questions