What is Equatable package in Dart and Flutter

Jehad Nasser
Flutter Community
Published in
2 min readJul 11, 2020

What is Equatable?

Assuming your father gives you and your brother 2 gifts, both gifts are laptops but they are not the same type of laptops; and because of curiosity or jealousy or whatever, you want to know if both gifts equal or not! So you decided to compare all aspects that important to you Processor, Memory, Storage. On paper, you will write something like this:

Two Laptops specifications

Assuming your brain uses Dart language then you will thought of each gift as an object(instance) of this class:

To compare Equality of both gifts which created using the class above “LaptopGiftClass”; Dart and other Object oriented languages (e.g Java, C#..) expect you to create(override) some functions to help these languages to understand the class you are writing, and then being able to compare any two objects of that class. See the uncommented code:

If these lines scares you off, no one blames you. That’s exactly why some nice people have created equatable package for us!

Equatable package is telling you “leave this scary job for me and enjoy your life with your objects”. But how to delegate the scary job to equatable package??! Nice question🤥, you can do so by doing two things:

  1. Edit your class to extend Equatable:
    class LaptopGiftClass extends Equatable {...}
  2. Return all class’ properties that you need to compare with as an array by Overriding this getter function which belongs to the Equatable:
    @override
    List<Object> get props => [processor, memory, storage];
    The Properties list [processor, memory, storage] is what you need to worry about as it’s the only thing you need to take care of.

So your class should be something like this:

AND YOU ARE DONE! Now you can check the equality of the two gifts easily, all you need to do is to create the objects then compare:

That’s it!!🤩 The older version of the Equitable package used to be a bit more complicated to understand, which I explained in this answer on StackOverFlow. This Article is an update for that answer.

https://twitter.com/FlutterComm

--

--