Sub topic 5
What freezed generates for you
This chapter assumes you know Dart classes, named parameters, null safety, and the basic freezed setup with build_runner. The examples use a generated file, so run the generator after changing the class declaration.
A freezed class is a convenient way to describe an immutable value object. You write the public declaration, and code generation adds the repetitive members that are easy to get wrong by hand.
Equality compares values
Dart's default equality compares object identity. Two separate objects with the same fields are usually not equal:
final first = Person(name: 'Ada', age: 36);
final second = Person(name: 'Ada', age: 36);
print(identical(first, second)); // false
print(first == second); // false without generated equalityfreezed overrides == so that instances compare their field values. It also generates a matching hashCode.
const first = Person(name: 'Ada', age: 36);
const second = Person(name: 'Ada', age: 36);
print(first == second); // true
print(first.hashCode == second.hashCode); // trueThis matters when Flutter compares state objects, when you use models as map keys, and when tests compare expected objects with actual objects.
`copyWith` creates updated values
Immutable objects do not change their fields after construction. Instead, copyWith creates a new instance with selected fields replaced.
const ada = Person(name: 'Ada', age: 36);
final olderAda = ada.copyWith(age: 37);
print(ada.age); // 36
print(olderAda.age); // 37Fields that you do not provide keep their original values. This makes state updates explicit and avoids mutating an object that another part of the application may still be using.
`toString` makes debugging easier
freezed generates a readable toString implementation. A log contains field names and values instead of only a class name and memory-like identifier.
print(olderAda);
// Person(name: Ada, age: 37, nickname: null)The exact output reflects the fields in the generated class. This is useful when inspecting Flutter state during development. Do not treat this output as a stable serialization format. Use JSON support when data must cross a process or be stored.
Generated members at a glance
| Generated member | Purpose |
|---|---|
| `==` and `hashCode` | Compare objects by their field values |
| `toString()` | Produce readable diagnostic text |
| `copyWith` | Create a changed instance without mutation |
| Constructors and fields | Build the implementation behind your declaration |
What the generated code does not do
The generated code does not choose how your application stores state, validate arbitrary business rules, or perform deep cloning of every object. It follows the declarations and annotations in your class.
For example, changing a field inside a list can still mutate that list:
final names = <String>['Ada'];
final people = Group(names: names);
names.add('Grace'); // The list itself was mutated.Prefer unmodifiable collections or immutable collection types when collection contents must not change. Use copyWith to replace the whole collection instead of editing it in place.
Exercise
Why can two separate Person instances with the same field values compare equal in a freezed model?
Show answer
freezed generates == and hashCode implementations that compare the declared field values rather than the objects' identities.