Updated 29 September 2023
Equatable in Flutter is a package used to compare the object.
If we want to compare objects normally then we have to override the ==
operator as well as hashCode
.
Overriding == operator is a lengthy and very tedious process as we need to write extra code for this.
If we fail to override it correctly it will not behave as we expect.
So, if you want to compare objects then Equatable is a convenient package for defining classes.
Classes can be compared for equality based on their properties rather than their identity. This is especially useful when you want to compare two instances of a class based on their content rather than their memory address.
Read more about our Flutter app development services.
First of all, we need to add a dependency to pubspec.yaml
file.
Let’s take a simple example to understand the scenario.
Person. dart
If we compare the above objects they are not the same the reason for this is Dart doesn’t compare the objects by their value.
Now, Let’s use Equatable in Person class.
In this blog, we have tried to explain how we can use Eqatable for comparison.
Thanks for reading this blog. Feel free to share your ideas or any feedback.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.