I have a collection of dishes.
I want to retrieve all restaurant who have a specific list of dish in his menu.
My data model is this:
restaurant---->rest1
| |-->menu
| | --> 1: true
| | --> 2: true
| | --> 3: true
|--->rest2
| |-->menu
| | --> 1: true
| | --> 2: true
| | --> 3: true
|--->rest3
| |-->menu
| | --> 1: true
my list of dishes is [1,2] for this reason I want to retrieve only rest1
and rest2
my code is this:
Future loadRestaurantsByDishes({List idPiatti})async{
idPiatti.forEach((element) async {
dynamic result2 = await _restaurantServices.getRestaurantOfDish(id_piatto: element["dishId"].toString());
rest.add( result2);
});
if(rest.length >0){
List<RestaurantModel> mom = [];
List<RestaurantModel> temp = [];
rest.forEach((item) {
if(mom.isEmpty){
mom.addAll(item);
}else{
temp.addAll(item);
mom.removeWhere((element) => !temp.contains(element));
temp = [];
}
});
notifyListeners();
}
}
Future<List<RestaurantModel>> getRestaurantOfDish({String id_piatto}) async =>
_firestore.collection(collection).where("menu."+id_piatto, isEqualTo: true).get().then((result) {
List<RestaurantModel> restaurants = [];
for (DocumentSnapshot restaurant in result.docs) {
restaurants.add(RestaurantModel.fromSnapshot(restaurant));
}
return restaurants;
});
My idea is to retrieve all resturant who made a specific dish and after retrieve the common elements between those lists in order to retrieve the only restaur ant which have all of them.
The problem is that mom
in the first statement is equal to item, but when I run mom.removeWhere((element) => !temp.contains(element));
it returns an empty list.
Where I'm getting wrong?
Thank you