Mohammed Bekele
Mohammed Bekele

Reputation: 1255

build runner package in my flutter app for JSON serialization not working

I'm using build_runner package in my flutter app for JSON serialization. I've correctly installed it in pubspec.yaml file under dev dependencies. but when I try to use flutter pub run build_runner build it releases the following.

    C:\Users\MoBix\Music\islamic_social_media>flutter pub run build_runner build
Unhandled exception:
Bad state: Unable to generate package graph, no `C:\Users\MoBix\Music\islamic_social_media\.dart_tool\flutter_gen\pubspec.yaml` found.
#0      _pubspecForPath (package:build_runner_core/src/package_graph/package_graph.dart:232:5)
#1      _parsePackageDependencies (package:build_runner_core/src/package_graph/package_graph.dart:206:21)
#2      PackageGraph.forPath (package:build_runner_core/src/package_graph/package_graph.dart:101:33)
<asynchronous suspension>
#3      main (file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-2.1.5/bin/build_runner.dart:27:30)
<asynchronous suspension>
pub finished with exit code 255

my pubspec.yaml

dev_dependencies:
  flutter_launcher_icons: ^0.9.2
  flutter_app_name: ^0.1.1
  build_runner:
  flutter_test:
    sdk: flutter

my flutter doctor -v

C:\Users\MoBix>flutter doctor -v
[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1348], locale en-US)
    • Flutter version 2.5.3 at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (6 weeks ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\MoBix\AppData\Local\Android\sdk
    • Platform android-Sv2, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.45
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 94.0.992.50

• No issues found!

What is the fix for this?

Upvotes: 13

Views: 10857

Answers (5)

code8x
code8x

Reputation: 1970

I got the same exception but my solution was strange:

I generated localization code and files first with:

flutter gen-l10n 

and then generating others with:

flutter pub run build_runner build   

worked seamlessly.

Upvotes: 1

starball
starball

Reputation: 50345

There's a pretty popular answer on GitHub by Ali Yazdi where they suggest to remove the l10n.yaml file, and then run flutter clean and then run flutter packages pub get.

Upvotes: 0

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

You might want to add flutter_gen to dev_dependencies to override the embedded one.

Upvotes: 1

selan
selan

Reputation: 1235

In my case this was happening to me trying to generate the code for testing using mockito and the annotation:

@GenerateMocks([ClassToMock])

The errorappear trying to do flutter pub run build_runner build and the <...>/.dart_tool/flutter_gen/ path doesn't exist.

The solution was creating an empty .dart_tool/flutter_gen/pubspec.yaml file:

mkdir .dart_tool/flutter_gen && touch .dart_tool/flutter_gen/pubspec.yaml

And fullify it with (as pointed here):

name: test
description: A new Flutter application.
version: 1.0.0

dependencies:

dev_dependencies:

Upvotes: 9

dumazy
dumazy

Reputation: 14435

It's interesting that it points to .dart_tool/flutter_gen/pubspec.yaml. This issue mentions this problem when generate: true is set in the pubspec.yaml.

If you don't need that, try removing it. Also, a flutter clean and flutter pub get might be necessary.

Upvotes: 38

Related Questions