55

flutter version:

flutter_macos_v1.9.1+hotfix.2-stable

create new project in terminal:

flutter create myapp

open vscode, edit pubspec.yaml:

dependencies:
  json_annotation: ^3.0.0

dev_dependencies:
  build_runner: ^1.7.0
  json_serializable: ^3.2.2

get packages in terminal:

flutter pub get

new /lib/user.dart and filling below:

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User extends Object {
  @JsonKey(name: 'seed')
  String seed;

  @JsonKey(name: 'results')
  int results;

  @JsonKey(name: 'page')
  int page;

  @JsonKey(name: 'version')
  String version;

  User(
    this.seed,
    this.results,
    this.page,
    this.version,
  );

  factory User.fromJson(Map<String, dynamic> srcJson) =>
      _$UserFromJson(srcJson);

  Map<String, dynamic> toJson() => _$UserToJson(this);
}

run flutter pub run build_runner build in terminal:

[INFO] Generating build script...
[INFO] Generating build script completed, took 321ms

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 10.4s

[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 698ms

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 2ms

[INFO] Running build...
[SEVERE] json_serializable:json_serializable on lib/user.dart:

Invalid argument(s): Path must be absolute : dart:core
[SEVERE] json_serializable:json_serializable on lib/main.dart:

Invalid argument(s): Path must be absolute : dart:core
[SEVERE] json_serializable:json_serializable on test/widget_test.dart:

Invalid argument(s): Path must be absolute : dart:core
[INFO] Running build completed, took 1.5s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 36ms

[SEVERE] Failed after 1.6s

why never succeeded?!

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Smeegol
  • 2,014
  • 4
  • 29
  • 44
  • 1
    Describe what you try to achieve instead of only posting the code, that would help others understand the question. Do not try to bypass stackoverflow restrictions – Cleptus Sep 20 '19 at 07:56
  • Dart 2.0 has some sort of bug with that same error message. https://github.com/dart-lang/sdk/issues/33551 – Cleptus Sep 20 '19 at 08:00
  • He's trying to automage Json serialization, as i am, and running into the same kind of error. Doesn't make any sense. This is what we're following: https://flutter.dev/docs/development/data-and-backend/json#serializing-json-using-code-generation-libraries HELP – morgred Sep 20 '19 at 11:32
  • Did you resolve this? I have the same issue. I cannot figure it out. – Oliver Dixon Sep 20 '19 at 12:12
  • @bradbury9 automated json serialization/deserialization, check my other comment – morgred Sep 20 '19 at 12:58
  • I'm having the same issue using Moor. :\ – Reda Lazri Sep 22 '19 at 10:43
  • make sure the class name is same with the generated file name. For ex: part 'User.g.dart'; class User { } – Mr B Dec 25 '20 at 17:02

18 Answers18

100

Try this.

flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs
VasilKanev
  • 1,392
  • 1
  • 6
  • 9
  • 1
    For web version, just append web... `flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs web` – Marcello DeSales Dec 27 '21 at 22:59
  • For some reason, I am forced to run flutter pub get every time even though there isn't anything to get. But it's just a minor inconvenience... – Jagadish Nallappa Jun 12 '22 at 11:01
43

add dependency in pubsec.yaml, analyzer: '0.39.14'

flutter clean
flutter pub cache repair
flutter pub run build_runner clean

Then run,

flutter pub run build_runner build
Noorus Khan
  • 1,342
  • 3
  • 15
  • 33
24

I tried with many solutions, But error not gone. flutter packages pub run build_runner watch command ran with endless log.

I deleted pubspec.lock and ran flutter pub get and installed dependency again and ran above command. After this error gone.

Supun Sandaruwan
  • 1,814
  • 19
  • 19
7

I had the same issue, so I just saved the changes first in the class (in your case User class).
And then I just retried using:

flutter pub run build_runner build
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
  • This answer worked for me. I was stuck trying to get `graphql_codegen` building and the docs say `dart run build_runner build`, but even swapping out _dart_ with _flutter_ wouldn't work. The trick was adding _pub_ just like you have it here: `flutter pub run build_runner build` – chemturion Nov 28 '22 at 23:10
6

Please create default empty constructor for models class before flutter packages pub run build_runner build command

Suhad Bin Zubair
  • 717
  • 7
  • 10
3

It might not be the case in this situation, but I had a similar problem caused by my auto formatter removing this line:

part 'my_class.g.dart';

Once I added that line and ran the command again it worked fine.

Andy
  • 723
  • 9
  • 24
3

I got this to work with the latest build_runner and json_serializable versions after a long process of trying all of the above suggestions: build_runner: ^1.10.2 json_serializable: ^3.4.0 Not sure what ultimately worked, but looks like one of the issues in my case was a slightly outdated dart SDK, so that's one more thing to keep an eye on

yulkin
  • 31
  • 1
  • Thanks for this answer! build_runner: ^1.10.2 requires Dart version 2.-10.0+, therefore, I changed the version to 1.10.1, and luckily it worked. – Almett Oct 01 '20 at 10:18
2

I had the same issue.

Successfully generating all *.g.dart files with:

build_runner 0.9.2

json_serializable 0.5.8+1

json_annotation 0.2.9+1

Karthikc
  • 31
  • 2
2

Looks like Analyzer is breaking it, downgrading to analyzer: 0.38.2 solved it for me.

Source: https://github.com/dart-lang/sdk/issues/38499#issuecomment-533812652

Reda Lazri
  • 131
  • 3
1

I had the same error. I simply installed the build runner package in pubspec.yaml file like -

dev_dependencies:

build_runner: ^1.3.1

mobx_codegen: ^0.3.9

samin
  • 482
  • 5
  • 9
1

you may need hive_generator in your dependencies

0

Update 2020/8/24: seems to break build_runner or json_serializable in version:

Analyzer: 0.39.16 

Going back to dart analyzer version:

Analyzer: 0.39.14 

fixed it for me. So, something is broken in 0.39.16.

Jiten Basnet
  • 1,623
  • 15
  • 31
  • i had an analyzer issue as well, however i wasn't as far as i was aware using it. Neither had i specified a versino to use. So i assumed that this was a cache issue and ran flutter clean and flutter pub get to hopefully reset the cache. I also found out that intellij had automatically commited my .g.dart files, which breaks the runner. So could be erroneous reporting. Who knows. – Emile Nov 02 '21 at 15:40
0

make sure the class name is same with the generated file name. For ex:

part 'User.g.dart';

class User {

}

אורי orihpt
  • 2,358
  • 2
  • 16
  • 41
Mr B
  • 99
  • 1
  • 7
0

My class name was:

class Game

but my filename was game_model.dart So you'll need to use:

part 'game_model.g.dart';

You may also need to add hive_generator dependency.

tazboy
  • 1,685
  • 5
  • 23
  • 39
0

Looks like flutter clean is resolving the issue. Try running it before i.e. openApi Generator.

Cyber
  • 2,194
  • 4
  • 22
  • 41
0

This is working you should definitely try it

I was getting the message "Success after 1.0 seconds with 0 output (0 actions)" when I used it with dart frog. He needed to create a "bin" file and put the factories in it so he could see it. This worked for me!

Image of Code

Throvn
  • 795
  • 7
  • 19
merterkoc
  • 1
  • 1
  • Please copy and paste the precise code or file tree here directly. Images are hard to read, have bad accessibility and deliver a lot of unnecessary information. – Throvn Mar 04 '23 at 16:40
0

In my side I edited the build_runner_core-version package source code outside my flutter project with adding a method to the class BuildForInputLogger in the following code below:

Stream<Level?> get onLevelChanged {

return Stream.empty(); }

And yeah it's worked to me

0

First make sure to maintain small letter and underscore style here:

part 'course_box.g.dart';

Then, run this:

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

It worked for me.