Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

dart run build_runner build fails or produces no generated file

ErrorTarget 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.

  • Applies to Flutter 3.50 stable, Dart 3.11, build_runner 2.x, json_serializable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-outputs
  2. 2Match what you see, then jump to its fix

    1. 1Target of URI hasn't been generated: 'user.g.dart'.Nothing has been generated yet, or the part line is wrongFix
    2. 2The build fails after generated files were committed or left from an earlier runOld generated files conflict with the new outputFix
    3. 3Adding or upgrading build_runner is refused during pub getThe Dart SDK is older than this build_runner needsFix

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 | head

2Fix

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 build

3Verify

user.g.dart now exists beside user.dart.

ls lib/**/*.g.dart

Confirmed by docs.flutter.dev

Did this fix it?

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_runner

2Fix

Delete generated outputs and the build cache, then build again.

dart run build_runner clean
dart run build_runner build --delete-conflicting-outputs

3Verify

The build ends with a success message and the .g.dart files are fresh.

git status --short | grep .g.dart

Confirmed by pub.dev

Did this fix it?

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 --version

2Fix

Upgrade Flutter so its Dart SDK is new enough, or keep an older build_runner that your Dart SDK supports.

flutter upgrade
flutter pub get

3Verify

pub get succeeds and the build runs.

dart run build_runner build

Confirmed by pub.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.