Search code examples
listdartcomparisoncustom-objectobject-comparison

How to use object comparision for custom objects in dart/flutter


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


Solution

  • While comparing objects of custom classes you have created, you must override the == override and the hashCode function.

    You can use the below explained method for you own custom classes in order to compare two of them using the == operator.

    Try running this inside a DartPad.

    class Cat {
      String id;
      Cat(this.id);
    
      @override
      bool operator == (Object other){
        return other is Cat && id == other.id;
      }
    
      @override
      int get hashCode => id.hashCode;
    
      @override
      String toString() => '{ id: $id }';
    
    }
    
    void main() {
      List l1 = [Cat('1'), Cat('2')];
      List l2 = [Cat('2'), Cat('3')];
      List l3 = [Cat('2'), Cat('4')];
    
      l1.removeWhere((item) => !l2.contains(item));
      l1.removeWhere((item) => !l3.contains(item));
      print(l1);
    }