What is build_runner in Flutter and how do you use it?

← Flutter answers

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.0
dart 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-outputs and, if needed, clean first.
  • Keep generator packages in dev_dependencies; only the annotation package belongs in dependencies.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required