2

How can we use multiple arrays contained in firestore? i want to filter sub-categories but giving error you can not use multiple array contain any or i am stuck on this step to filter category and sub category can you suggest me how can I model data or programmatically query multiple where

Future<List<ProductModel>> filterproductswithsubcategory(String category,String subcategory) async {
  QuerySnapshot productsSnapshot =
  await Firestore.instance.collection('products').where('categories',
      arrayContains:"Men").where('subcategories',
      arrayContains:"Shirt").getDocuments();

  List<ProductModel> productModelList = [];

  for (DocumentSnapshot documentSnapshot in productsSnapshot.documents) {
    ProductModel productModel = ProductModel.fromMap(
        documentSnapshot.documentID, documentSnapshot.data);
    productModelList.add(productModel);
  }
  return productModelList;
}
Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
Sanjeev Baria
  • 158
  • 2
  • 9
  • 3
    Since you're already correctly stated that you **can't** use multiple array-contains in a query, what are you really asking? If you're asking how to restructure your data to support some kind of query, I suggest editing the question to restate your goal. Be clear about the requirements of your query. – Doug Stevenson Jul 28 '20 at 17:54
  • i am stuck on this step to filter category and sub category can you suggest me how can I model data or programmatically query multiple where – Sanjeev Baria Jul 28 '20 at 18:36
  • 1
    Could you **edit the question** to be more clear? Don't just leave your requirements in comments - the question should be the source of your problem. – Doug Stevenson Jul 28 '20 at 19:04
  • sorry for a mistake just started stack overflow so – Sanjeev Baria Jul 28 '20 at 20:08
  • As you already know, you can only have one `array-contains` condition in a query. The only option I see is to use a single field for both the category and the subcategory, and then use the approach Doug outlined here: https://stackoverflow.com/questions/54987399/firestore-search-array-contains-for-multiple-values – Frank van Puffelen Jul 28 '20 at 21:23
  • Any update on this @Sanjeev – Jigar Fumakiya Oct 07 '20 at 11:41

0 Answers0