Dart: Equatable

Utsav Ghimire
4 min readAug 10, 2022

Equatable is a protocol that allows two objects to be compared using the == operator. The hashValue is used to compare two instances.

https://github.com/felangel/equatable/raw/master/doc/assets/equatable_logo_full.png

While developing an application, we have been in the condition where we often have to compare two objects using an == operator whether they are equal or not.

For comparing two objects, dart provides two methods == and hashValue. By overriding these two methods, we can compare two object instances.

This article solely focuses on the difference between using hashValue and == operator for comparing two objects, advantages and drawbacks, and why we need to use the Dart Equatable package.

Let’s tie the buckle of the belt and start.

Using equality operator

Let us take an example by creating Person class

Now let us create persons and compare them.

Now try running your program and check the output.

Although, person1 and person3 are equal, the program prints all the statements as false.

dart equality operator
gif from tenor gif https://tenor.com/search/odd-gifs

This is quite strange. But why?

Dart doesn't compare two objects by their value; instead, it compares their memory location. As all person objects have different memory locations, regardless of their values, they are considered unequal objects by the dart.

Now let us introduce the two override methods that dart provides us. Let us override hashCode and == methods.

Now let us run the code once again.

What just happened here?

Now the == operator compares the objects by their types and values. Hence, person1 and person3 are equal.

Although it is doing a great job, we need to compare each and every field manually. And this becomes quite verbose and tedious when we need to deal with complex classes.

image credit: internet

This is where Equatable arrives.

What does Equatable do?

Equatable overrides == and hashCode for you so you don't have to waste your time writing lots of boilerplate code.

There are other packages that will actually generate the boilerplate for you; however, you still have to run the code generation step which is not ideal.

With Equatable, there is no code generation needed and we can focus more on writing amazing applications and less on mundane tasks.

Now let our class Person extend Equatable and override the getter method props

You simply have to return the list of all objects that are available in your class. In our case, it's name, age, and phone.

Now, let us re-run the code.

output

We can now compare all the instances of Person just like before without the pain of having to write all of that boilerplate.

Note: Equatable is designed to only work with immutable objects so all member variables must be final.

When we print person1 object, we get Person as output.

Equatable has one additional functionality, stringify method.

Now, when we print person1 object, we get

Person(Utsav, 23, 987654321)

Learn more about Equatable on pub.dev

Get full source code on Github:

You can reach me out:

--

--