talira

equatable

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode

open sourceMITfree

equatable gives Dart and Flutter classes value-based equality: a class extends Equatable (or mixes it in) and lists the fields that matter in props, and equatable implements == and hashCode from them. It can also generate toString from the same fields, per class with stringify or globally with EquatableConfig. It is meant for immutable classes, and works in Flutter and server-side Dart alike. Felix Angelov maintains it.

Problems it solves

  • Comparing two Dart objects with the same fields returns false
  • I am tired of hand-writing == and hashCode overrides for every Flutter model and state class
  • My Flutter state updates are not detected because objects compare by identity

Features

  • Implements == and hashCode from a props list
  • Works as a base class or as a mixin
  • Optional toString built from props (stringify, or EquatableConfig globally)
  • Pure Dart, for Flutter and server-side Dart

Limitations

  • It is meant for immutable classes only: every field must be final, since hashCode on mutable values breaks hash-based collections.1

Capabilities

Has official SDKsplan not known

Alternatives

The same job

These do what equatable does.

  1. Freezed Code generation for immutable data classes and unions in Dart

Compare equatable with the top 3