Equatable package in dart

Shan - Feb 3 '23 - - Dev Community

The equatable package in Flutter is a powerful tool for comparing objects. It allows developers to easily determine if two objects have the same properties and values, which can be especially useful when working with complex data structures or implementing value types in their Flutter apps.

The equatable package provides a base Equatable class that we can extend to create our own custom value types. To use the package, we need to import it in our dart file and extend the Equatable class.

Here is an example of how to use equatable to create a custom value type called Person:

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  final String name;
  final int age;

  Person({required this.name, required this.age});

  @override
  List<Object> get props => [name, age];
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Person class extends Equatable and implements the props getter. The props getter is used to specify which properties of the object should be used to determine equality. In this case, the name and age properties of two Person objects are being compared.

Once you have defined your custom value type, you can use the == operator to compare two instances of that type:

final person1 = Person(name: 'John', age: 30);
final person2 = Person(name: 'John', age: 30);

print(person1 == person2); // true
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can use the isEquals method to compare two instances of your custom value type:

final person1 = Person(name: 'John', age: 30);
final person2 = Person(name: 'John', age: 30);

print(Equatable.is(person1, person2)); // true
Enter fullscreen mode Exit fullscreen mode

You can also use equatable package to compare a list of objects.

final people1 = [
  Person(name: 'John', age: 30),
  Person(name: 'Mike', age: 25),
];

final people2 = [
  Person(name: 'John', age: 30),
  Person(name: 'Mike', age: 25),
];

print(Equatable.listEquals(people1, people2)); // true
Enter fullscreen mode Exit fullscreen mode

The equatable package uses the == operator and the hashCode method to determine equality between objects. When the == operator is called on two objects, equatable compares the values of the properties specified in the props getter to determine if they are equal. If the props getter is not implemented or is empty, equatable will use the hashCode method to compare the objects.

It’s important to note that equatable only compares the properties that you specify in the props getter. If you need to compare other properties or custom logic, you should implement the == operator and hashCode in your custom class.

Using equatable package can make your code more robust and maintainable, as it allows you to easily compare complex data structures and value types. It can be especially useful when working with large and complex data structures, as it provides an easy and consistent way to determine equality between objects.

Another important feature of the equatable package is that it can help to improve the performance of your app. When you use the == operator to compare two objects, the equatable package will only compare the properties specified in the props getter, rather than comparing all properties of the object. This can greatly reduce the amount of computation required to determine equality, especially when comparing large data structures or collections of objects.

Additionally, the equatable package can be used in conjunction with other popular Flutter packages, such as provider and bloc, to help manage and compare the state of your app. For example, you can use the equatable package to compare the state of a provider or bloc before and after an update, and only rebuild the parts of your app that have changed.

In summary, the equatable package is a powerful tool for comparing objects in Flutter. It provides a simple and consistent way to determine equality between objects, and can greatly improve the performance of your app when working with complex data structures or collections of objects. Whether you are working on a simple app or a large and complex project, the equatable package is a valuable tool to have in your toolbox.

. . . . . .