I am working on an app where I have to get products from the firestore by the name of a category. I am new to flutter and don't know how to do that. Here is my firebase layout.
CodePudding user response:
Yes, there is a way to filter out docs with their types or properties in firebase. In your case, the firebase firestore structure is not clearly visible. But you can try out the below code or can make changes to that code. If you want continuous data you can make the stream function for that, else you can make it future. Check out the below code for reference.
Stream<QuerySnapshot<Map<String, dynamic>>> getProductsByCategories(
String categoryName) {
Stream<QuerySnapshot<Map<String, dynamic>>> snapshot = firestore
.collection('products')
.where('category', arrayContains: categoryName)
.snapshots();
return snapshot;
}
}