Sub topic 2
Value equality and the boilerplate it requires
Assume you know Dart classes, named constructors, final fields, and null safety. This chapter uses those features to explain how objects compare in Dart and why immutable data classes need generated equality.
Identity equality versus value equality
Dart objects have an identity. Identity answers, “Are these two references pointing to the same object?” The default == behavior usually compares identity.
class Person {
const Person({required this.name, required this.age});
final String name;
final int age;
}
void main() {
const first = Person(name: 'Ada', age: 36);
const second = Person(name: 'Ada', age: 36);
print(identical(first, second)); // false
print(first == second); // false
}The two Person objects contain the same data, but they are separate instances. For application models, comparing their contents is usually more useful. This is called value equality. Two objects are equal when their relevant fields have equal values.
| Comparison | Question | Typical result for equal fields |
|---|---|---|
| Identity | Are these the same instance? | `false` |
| Value | Do these instances represent the same data? | `true` |
Value equality matters when Flutter decides whether state changed. It also makes tests clearer. A test can compare an entire model instead of checking every field separately.
The manual boilerplate
Dart lets a class define value equality by overriding == and hashCode.
class Person {
const Person({required this.name, required this.age});
final String name;
final int age;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Person && other.name == name && other.age == age;
@override
int get hashCode => Object.hash(name, age);
@override
String toString() => 'Person(name: $name, age: $age)';
}
void main() {
const first = Person(name: 'Ada', age: 36);
const second = Person(name: 'Ada', age: 36);
print(first == second); // true
print(first.hashCode == second.hashCode); // true
print(first); // Person(name: Ada, age: 36)
}The identical check handles the case where both references point to the same object. The type check prevents a Person from comparing equal to an unrelated type. The field comparisons define which data contributes to equality.
hashCode must use the same fields as ==. Collections such as Set and Map depend on this rule. If two objects compare equal, they must have equal hash codes.
toString is not required for equality, but it makes logs and test failures readable. Adding another field means updating every relevant method. Omitting a field creates a subtle bug: objects that differ in that field can compare equal.
How freezed removes repetition
freezed generates ==, hashCode, toString, and other members from one field declaration. You describe the model once, so adding or removing a field does not require manually synchronizing several methods.
Exercise
Exercise
Why must hashCode use the same fields that == compares?
Show answer
Hash-based collections require equal objects to have equal hash codes. Using different fields can make equal objects behave incorrectly in sets or maps.