In this blog post you will learn about Equatable and also normal comparison operator. The usage of Equatable is more efficient and recommended if you are working with a class that needs to compare objects.
Normal Comparison
In Dart, the default way to compare two objects for equality is to use the ==
operator. This operator compares the references of the two objects. If the references are the same, the objects are considered equal. If the references are different, the objects are considered not equal.
For example, the following code compares two Person
objects for equality:
Person person1 = Person("Alice", 20);
Person person2 = Person("Alice", 20);
bool areEqual = person1 == person2;
In this example, areEqual
will be true
because the references of person1
and person2
are the same.
However, there are some cases where the ==
operator will not work as expected. For example, the following code will also return true
even though the two Person
objects are not the same:
Person person1 = Person("Alice", 20);
Person person2 = Person("Alice", 20);
person2 = person1;
bool areEqual = person1 == person2;
In this example, person2
is assigned the reference of person1
. So, even though the two Person
objects are not the same, the ==
operator will still return true
because they have the same reference.
Equatable
The Equatable protocol is a way to define how to compare two objects for equality. When a class conforms to the Equatable protocol, it must override the operator ==
and hashCode
methods. The operator ==
method is used to compare two objects for equality, and the hashCode
method is used to generate a hash code for an object.
The operator ==
method in the Equatable protocol should compare the values of the object’s properties, not the references of the object. This ensures that two objects are considered equal only if their values are equal.
For example, the following code shows how to use the Equatable protocol to compare two Person
objects for equality:
class Person extends Equatable {
final String name;
final int age;
Person({this.name, this.age});
@override
bool operator ==(Object other) {
if (other is Person) {
return name == other.name && age == other.age;
}
return false;
}
@override
int get hashCode => name.hashCode ^ age.hashCode;
}
In this example, the operator ==
method compares two Person
objects for equality by comparing their names and ages. The hashCode
method generates a hash code for a Person
object by combining the hash codes of its name and age.
Comparison
The following table compares the normal comparison and the Equatable protocol:
Comparison method | Description |
---|---|
== operator |
Compares the references of the objects. |
Equatable protocol | Compares the values of the objects’ properties. |
Benefits |
| * Simpler and easier to read code. | * Can improve the performance of the code. | Drawbacks | | * Requires more code to implement. | * Not as flexible as the ==
operator.
Conclusion
The Equatable protocol is a powerful tool that can be used to simplify the process of comparing objects for equality. It is a good choice for classes that need to be compared for equality frequently. However, the ==
operator is still a valid way to compare objects for equality. It is a good choice for classes that do not need to be compared for equality frequently.
I hope this blog post has been helpful. If you have any questions, please feel free to ask me in the comments.