Could not find a file named "pubspec.yaml" when auto generating code with build_runner

Viewed 4889

After I have updated Flutter to v1.22 using code generation with build_runner was not working. I was told that the proper command to use in this version was:

dart pub run build_runner watch --delete-conflicting outputs

However, I receive the following error:

Could not find a file named "pubspec.yaml" in "C:\Users\jpiab\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org\_fe_analyzer_shared-2.2.0".

I have no idea why it is looking for pubspec.yaml in that folder, since that folder is not the current working directory.

--- EDIT ---

The file exists in path: C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/_fe_analyzer_shared-2.2.0

dart is just looking for it in the wrong place. Any ideas on how to fix it?

4 Answers

I solved for me. I use Android Studio in Ubuntu 20.04.01 LTS.

Firstly run this command:

flutter pub get

After run this command:

flutter packages pub run build_runner build

Or:

flutter packages pub run build_runner watch

Source: https://github.com/flutter/flutter/issues/50092

Deleting the .pub-cache folder. and then run pub get

Edit:
If above method won't work then delete the folder

C:\Users\jpiab\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org_fe_analyzer_shared-2.2.0

and then run pub get from within your project directory.

That same error I had and I resolved it by aligning my dependencies. Check if your build_runner is in the same line as flutter_test if you added it to dev_dependencies.

Error Corrected

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: [latest]

My Error

dev_dependencies:
  flutter_test:
    sdk: flutter
    build_runner: [latest]

I fixed it by changing the environment variable PUB_CACHE to the right folder.

Related