EqualsAndHashCode mixin

Mixin-style contract for value-based equality, hashCode, and toString.

Implementations must override equalizedProperties to return the list of values that uniquely define the identity of this object. These values are then used by the central equalizer utility to implement:

  • operator == → deep equality across selected properties
  • hashCode → stable hash based on property order
  • toString → human-readable representation

Rules

  • Always include all properties that define identity.
  • The order of properties matters for hashCode.
  • null values are supported and compared safely.
  • Exclude transient or derived values (only core identity).

Example

class User with EqualsAndHashCode {
  final String id;
  final String name;

  User(this.id, this.name);

  @override
  List<Object?> equalizedProperties() => [id, name];
}

void main() {
  final a = User('1', 'Alice');
  final b = User('1', 'Alice');

  print(a == b);         // true
  print(a.hashCode == b.hashCode); // true
  print(a);              // User(id=1, name=Alice)
}
Mixin applications

Properties

hashCode int
The hash code for this object.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

equalizedProperties() List<Object?>
Mixin-style contract for value-based equality, hashCode, and toString.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override