Maaz Zindani

Loading the index…

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

dart run build_runner says it cannot find the package

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

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

Fix it fast

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

    1. 1grep prints nothing, so the package was never addedbuild_runner is not in pubspec.yamlFix
    2. 2build_runner is in pubspec.yaml, but pwd shows a sub-folder such as lib, android or the repository root above the appThe command runs from the wrong folderFix

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

2Fix

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_serializable

3Verify

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

Confirmed by docs.flutter.dev

Did this fix it?

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

2Fix

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 build

3Verify

The build prints its actions and writes the .g.dart files next to your sources.

find lib -name "*.g.dart"

Confirmed by pub.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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