dart run build_runner build fails or produces no generated file
Target of URI hasn't been generated: 'user.g.dart'.Short answer
build_runner only writes the generated file your code points to with a part directive, and it needs a Dart SDK new enough for its version. Run it with the dart command, from the project root. Stale outputs from an earlier run are cleared with --delete-conflicting-outputs or the clean command. Read the first error to tell the three apart.
Fix it fast
1Find out which cause you have
Run the build once with the command Flutter's docs use, from the project root, and read the first error.
dart run build_runner build --delete-conflicting-outputs2Match what you see, then jump to its fix
- 1
Target of URI hasn't been generated: 'user.g.dart'.Nothing has been generated yet, or the part line is wrongFix - 2
The build fails after generated files were committed or left from an earlier runOld generated files conflict with the new outputFix - 3
Adding or upgrading build_runner is refused during pub getThe Dart SDK is older than this build_runner needsFix
- 1
What does the error look like?
Target of URI hasn't been generated: 'user.g.dart'.pub get failed
Cause 1 of 3
Nothing has been generated yet, or the part line is wrong
1Check
This error is expected on the first run: the file does not exist until the build has run. If it stays after a successful build, the part directive must match the source file name exactly (user.dart needs part 'user.g.dart').
grep -rn "^part " lib | head2Fix
Add the matching part line, make sure json_annotation is in dependencies and build_runner plus json_serializable are in dev_dependencies, then build.
flutter pub add json_annotation dev:build_runner dev:json_serializable
dart run build_runner build3Verify
user.g.dart now exists beside user.dart.
ls lib/**/*.g.dartConfirmed by docs.flutter.dev
Cause 2 of 3
Old generated files conflict with the new output
1Check
Files generated by an earlier run, or committed to Git, can block a new build. The Flutter docs add --delete-conflicting-outputs for this. build_runner 2.7.0 made the -d flag a no-op and 2.16.0 changed the default to correcting generated files, so on recent versions the flag is harmless but may not be needed.
flutter pub deps | grep build_runner2Fix
Delete generated outputs and the build cache, then build again.
dart run build_runner clean
dart run build_runner build --delete-conflicting-outputs3Verify
The build ends with a success message and the .g.dart files are fresh.
git status --short | grep .g.dartConfirmed by pub.dev
Cause 3 of 3
The Dart SDK is older than this build_runner needs
1Check
Each build_runner release raises the minimum Dart SDK: 2.4.0 needs 3.0, 2.5.0 needs 3.7, 2.15.1 needs 3.8 and 2.15.2 needs 3.11. Compare with the Dart SDK inside your Flutter.
flutter --version2Fix
Upgrade Flutter so its Dart SDK is new enough, or keep an older build_runner that your Dart SDK supports.
flutter upgrade
flutter pub get3Verify
pub get succeeds and the build runs.
dart run build_runner buildConfirmed by pub.dev
How do I stop it coming back?
- Run builders from the project root with dart run build_runner, as the Flutter JSON guide does.
- Decide once whether generated files are committed, and keep that rule for the whole team.
- Use watch during development so generated files never go stale.
- Add .dart_tool to .gitignore; build_runner keeps private files there.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
flutter pub get fails and no packages are downloaded
Got socket error trying to find package
Run pub get in a terminal and read the first error.
4 causes, each with a fixMobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fix