Sub topic 3
Installing freezed and running build_runner
This chapter assumes you know Dart classes, named parameters, null safety, and basic Flutter project structure. You should also be comfortable running commands in a terminal. freezed uses Dart code generation, so the source file and the generated file work together.
What the packages do
freezed is a Dart package that generates immutable model code. It can create value equality, copyWith, and readable toString methods from a short class declaration.
freezed_annotation contains the annotations used in your Dart source. The freezed package contains the generator. build_runner runs generators and writes their output. json_serializable is an additional generator for JSON conversion.
Add the runtime packages as regular dependencies and the generators as development dependencies:
flutter pub add freezed_annotation json_annotation
flutter pub add --dev build_runner freezed json_serializableThe generated code is needed when the application runs, but the generator packages are only needed while developing or building the project. That is why the commands place them in different dependency sections.
Running the generator
Run this command from the directory containing pubspec.yaml:
dart run build_runner build --delete-conflicting-outputsIn a Flutter project, this equivalent command is also common:
flutter pub run build_runner build --delete-conflicting-outputsbuild performs one generation pass and then exits. The --delete-conflicting-outputs option lets build_runner replace generated files that conflict with its current output. Use it when generated files are stale or when you changed a model declaration.
During active development, use watch instead:
dart run build_runner watch --delete-conflicting-outputsThe watcher stays open and regenerates files when it detects source changes. Stop it with Ctrl+C.
| Command | Behavior | Typical use |
|---|---|---|
| `build` | Generates once and exits | CI or a single update |
| `watch` | Regenerates after file changes | Daily development |
| `clean` | Removes generated outputs tracked by the builder | Recovery from stale output |
Connecting a source file to generated output
A model file must declare the generated files with part directives. For a Freezed-only model, the source might begin like this:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'person.freezed.dart';
@freezed
abstract class Person with _$Person {
const factory Person({
required String name,
@Default(0) int age,
}) = _Person;
}The file must be named person.dart for the directive part 'person.freezed.dart'; to work. Run the generator after saving the file. It creates person.freezed.dart beside the source file.
If the class also has JSON factories, add a second directive:
part 'person.g.dart';The .freezed.dart file contains Freezed output. The .g.dart file contains JSON serialization output. Do not edit either generated file by hand. Change the source declaration and run the generator again.
The generation flow
Troubleshooting the setup
- Cannot find the generated mixin: Run build_runner and check that the
partfilename matches the source filename. - Conflicting outputs: Run the command with
--delete-conflicting-outputs. - No output appears: Confirm that the file contains a supported annotation such as
@freezedand that the command runs from the project root. - Import errors after changing packages: Run
flutter pub get, then run build_runner again. - The watcher seems stuck: Stop it, run a one-time
build, and inspect the first error in the terminal.
Exercise
You created lib/person.dart with part 'person.freezed.dart';. Which command performs one generation pass and replaces conflicting generated files?
Show answer
dart run build_runner build --delete-conflicting-outputs