build_runner runs code generators and writes the generated .g.dart files. Run dart run build_runner build --delete-conflicting-outputs, or watch to rebuild as you edit.
# pubspec.yaml
dependencies:
json_annotation: ^4.9.0
dev_dependencies:
build_runner: ^2.4.0
json_serializable: ^6.8.0dart run build_runner build --delete-conflicting-outputs # one-off
dart run build_runner watch --delete-conflicting-outputs # keep running
dart run build_runner clean # remove cached state- Generators read annotations in your code, so a file needs the matching
part 'name.g.dart';line. - Commit the generated files or generate them in CI, but be consistent across the team.
- If you see conflicting outputs or stale code, run with
--delete-conflicting-outputsand, if needed,cleanfirst. - Keep generator packages in
dev_dependencies; only the annotation package belongs independencies.
