felangel / equatable

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.
https://pub.dev/packages/equatable
MIT License
920 stars 102 forks source link

Why the equatable need to be @immutable? #134

Closed Katekko closed 2 years ago

Katekko commented 2 years ago

I'm asking because I'm trying to extend my class from other class that's not immutable. image

So I'm wondering why equatable needs to be marked as immutable?

Katekko commented 2 years ago

AVOID defining custom equality for mutable classes. Linter rule: avoid_equals_and_hash_code_on_mutable_classes

When you define ==, you also have to define hashCode. Both of those should take into account the object’s fields. If those fields change then that implies the object’s hash code can change.

Most hash-based collections don’t anticipate that—they assume an object’s hash code will be the same forever and may behave unpredictably if that isn’t true.