Dart – Compare two objects

Add two overrides to the model: The operator and hashCode.

void main() {
  Car c = Car(name: 'Audi', colour: 'Red');
  Car d = Car(name: 'Mercedes', colour: 'Red');
  
  print(c == d);
}

class Car {
  String name;
  String colour;
  
  Car({required this.name, required this.colour});

  @override
  bool operator ==(Object other) {
    return other is Car && name == other.name && colour == other.colour;
  }

  @override
  int get hashCode => name.hashCode ^ colour.hashCode;
  
   @override
  toString() {
    return('name: $name, colour: $colour');
  }
}