dart run build_runner says it cannot find the package
Could not find package "build_runner"Short answer
dart run can only start a program from a package your project depends on, and Flutter's docs list build_runner under dev_dependencies. Add it with flutter pub add, run pub get, then start the build from the folder that holds pubspec.yaml. Without the dependency or in a wrong folder, the package cannot be found.
Fix it fast
1Find out which cause you have
Check whether the package is in your pubspec at all, and which folder your terminal is in.
pwd grep -n "build_runner" pubspec.yaml2Match what you see, then jump to its fix
What does the error look like?
Could not find package "build_runner"Target of URI hasn't been generated: 'user.g.dart'
Cause 1 of 2
build_runner is not in pubspec.yaml
1Check
dart run <package> starts a program from a package the current package depends on. A package that is not listed in pubspec.yaml is not a dependency, so there is nothing to start.
grep -n -A6 "dev_dependencies" pubspec.yaml2Fix
Add the generator packages the way Flutter's JSON guide does. The dev: prefix writes them under dev_dependencies, which is where code generators belong.
flutter pub add dev:build_runner dev:json_serializable3Verify
pubspec.yaml now lists build_runner under dev_dependencies and the build runs.
grep -n "build_runner" pubspec.yaml
dart run build_runner build --delete-conflicting-outputsConfirmed by docs.flutter.dev
Cause 2 of 2
The command runs from the wrong folder
1Check
The package docs say to change to the package root before running build_runner. In a repository where the Flutter app sits in a sub-folder, the root of the repo is not the package root.
pwd
ls pubspec.yaml2Fix
Change to the folder that contains the pubspec.yaml of the app, fetch dependencies there, and run the build again.
cd path/to/your_app
flutter pub get
dart run build_runner build3Verify
The build prints its actions and writes the .g.dart files next to your sources.
find lib -name "*.g.dart"Confirmed by pub.dev
How do I stop it coming back?
- Add generators with flutter pub add dev:<package> so they land under dev_dependencies.
- Keep one command in your README: run dart run build_runner build from the folder with pubspec.yaml.
- Commit pubspec.lock for an app so every machine resolves the same build_runner.
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
dart run build_runner build fails or produces no generated file
pub get failed
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.
3 causes, each with a fixMobile 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 fix