Sub topic 4
Your first freezed class
Prerequisites and the goal
This chapter assumes you know Dart classes, named parameters, null safety, and how to run commands in a terminal. You should also have a Flutter or Dart project with freezed_annotation, freezed, and build_runner configured.
A freezed class is an immutable data class. Immutable means that an object cannot change after it is created. Instead of changing an existing object, you create another object with the new values. This makes Flutter state easier to reason about because each state value remains stable.
We will define a Person model with:
- A required
name - A required
age - An optional
nickname
Declare the class
Create lib/person.dart:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'person.freezed.dart';
@freezed
abstract class Person with _$Person {
const factory Person({
required String name,
required int age,
String? nickname,
}) = _Person;
}The part directive connects this file to the generated file. The generated file will contain the implementation details for Person.
The @freezed annotation tells the generator to create code for this class. The with _$Person clause connects your declaration to the generated mixin. The factory constructor describes the public API that you use when creating objects.
Run the generator from the project directory:
dart run build_runner build --delete-conflicting-outputsFor a Flutter project, flutter pub run build_runner build --delete-conflicting-outputs also works in projects that use that command style. The generator creates person.freezed.dart. Do not edit that generated file because your changes will be overwritten during the next build.
Create and use instances
After generation, use the class like an ordinary Dart class:
import 'person.dart';
void main() {
const ada = Person(
name: 'Ada',
age: 36,
);
const grace = Person(
name: 'Grace',
age: 28,
nickname: 'Amazing Grace',
);
print(ada.name); // Ada
print(grace.nickname); // Amazing Grace
}The fields are final in the generated implementation. Code outside the model cannot assign a new value to ada.name or ada.age.
The generated class also provides useful value-object behavior:
| Feature | Purpose |
|---|---|
| Named constructor parameters | Make object creation clear |
| Immutable fields | Prevent accidental mutation |
| `copyWith` | Create a changed copy |
| Value equality | Compare field values |
| `toString` | Produce useful diagnostic text |
You will examine these generated members in detail in the next chapter. For now, notice that your source file describes the model once, while generated code supplies the repetitive implementation.
Understand the generated structure
The relationship between your declaration and generated code looks like this:
Your class declaration is the source of truth. The generated implementation is a build output. When you change a constructor field, run the generator again so the output matches the declaration.
Check the result
Create an object with all required fields before adding optional data. If you omit name or age, Dart reports a compile-time error because both parameters are marked required.
A nullable field has a different meaning. String? nickname allows the field to contain either a String or null. It is still part of the object, but a nickname is not necessary when constructing a Person.
Exercise
What must you do after adding a new field to a freezed class?
Show answer
Run build_runner again so the generated implementation includes the new field and its related members.