Fix
Let the tool delete the old generated files:
dart run build_runner build --delete-conflicting-outputs
(Use flutter pub run build_runner ... on very old setups.) This happens when generated *.g.dart or *.freezed.dart files already exist and were produced by a different version, or were committed and then changed.
If it still fails
flutter clean && flutter pub get
- Delete the
.dart_tool/build folder.
- Run the command again with
--delete-conflicting-outputs.
- Read the first error, not the last: a syntax error in one source file blocks all generation.
Common causes
- Missing
part 'file.g.dart'; directive, or a typo in the part name (it must match the file name).
- A generator package version mismatch (for example
json_serializable with an old json_annotation). Keep json_annotation in dependencies and build_runner plus json_serializable in dev_dependencies, and upgrade them together.
- Running
flutter pub upgrade without regenerating.
Daily workflow
dart run build_runner watch --delete-conflicting-outputs regenerates on save.
- Decide as a team whether generated files are committed. Committing them makes CI simple; ignoring them keeps diffs small but requires a build step in CI (
dart run build_runner build before tests).
Pitfall
Do not edit *.g.dart by hand; the next build overwrites it. If a generator output is wrong, change the annotation or source class.