Home > Back-end >  Comparing between two same objects returning false
Comparing between two same objects returning false

Time:10-11

I have list of objects and I need to see if the new object is already in the list. So, I make condition if the list contains the new object, it's returning false ,even though I print the list and it contains an object with the same data of the new object maybe it is not being compared between objects depending on the data inside them. What's the solution For example I have this class:

class Favorite{
  final Text date;
  final Text time;
  final Text source;
  final Text destination;
  final Text price;

  Favorite(this.date, this.time, this.source, this.destination, this.price);

  // I used this override to print the data inside the object instead of printing 'instance of favorite' to compare the data
  @override
  String toString() {
    return '[$date, $time, $source, $destination, $price]';
  }
}

in other class:

List<Favorite> list = [];
Favorite favorite = Favorite(
  Text(Map<String, dynamic>.from(
      snapshot.value as Map)[Consts.pathDateJourney]),
  Text(Map<String, dynamic>.from(
      snapshot.value as Map)[Consts.pathTimeJourney]),
  Text(Map<String, dynamic>.from(
      snapshot.value as Map)[Consts.pathSourceCity]),
  Text(Map<String, dynamic>.from(
      snapshot.value as Map)[Consts.pathDestinationCity]),
  Text(Map<String, dynamic>.from(
      snapshot.value as Map)[Consts.pathPriceJourney]),);
list.add(favorite);
print(list.contains(favorite)); //returning false
print(list[0] == favorite); //returning false

the output of printing the list:

I/flutter (14458): []
I/flutter (14458): [[Text("2022-10-27"), Text("05:00"), Text("Homs"), Text("Hama"), Text("800 $")]]
I/flutter (14458): [[Text("2022-10-27"), Text("05:00"), Text("Homs"), Text("Hama"), Text("800 $")], [Text("2022-10-27"), Text("05:00"), Text("Homs"), Text("Hama"), Text("800 $")]]

CodePudding user response:

all objects in the dart language except the primitive data types like string, int, and double... are not equal to each other since no == operator is overridden and set to it, and this include also collections like lists, maps...

the Favorite class even if you didn't extend the Object class, is also an Object in dart

so:

Favorite() == Favorite() // false

unless you override the == operator of it to set when two objects should be considered equals like this :

class Favorite {
  final Text date;
  final Text time;
  final Text source;
  final Text destination;
  final Text price;

  Favorite(this.date, this.time, this.source, this.destination, this.price);


@override
bool operator ==(Object other) {
return other is Favorite && date.toString() == other.date.toString();
} 

now you just said that every Favorite class that has the same date.toString() should be equal.

you can set your equals logic in the == overriden method. (it's just an example).

so when you write

    Favorite fav1 = Favorite(/* other properties*/);
    Favorite fav2 = Favorite(/* other properties*/)
    fav1 == fav2

it will see if they have the same date so, if they are so then it's true otherwise it's false

now the algorithm you wrote should work.

  • Related