Search code examples
flutterdartpackagehashcodeequatable

Flutter Equatable Real Life Usage?


I have learned that what is the Equatable and how can I use it. But I just wonder that why should I use it?

I have found some reasons. One of them, when we wants to compare 2 or more same object from any class, it will use. they aren't same even if they properties same because of hash code. But equatable compares each other without hash code.

But I really wonder that where I will use in real life scenario?

Many Thanks..


Solution

  • By default 2 reference objects compare by references this means if you will create two equal objects and try to compare they will be different because references are different and you have manually check each field with each field of another object. For detailed information on equals and hasCode concept, you can read here.

    As a result, if the object doesn't have specific equals and hashCode then you can't use such objects in the Set, Map, HashTables and other data structures.

    Another real example of this is unit tests. It will be very exhausting for you to write tests where you have to compare actual result with expected ones because on each test you have to compare each field instead of just using the equivalency operator(==).

    Remember, this is not a dart feature this is a standard approach in computer science and data structures.