40

I'm just trying to generate my files with the same command i stored like 3 months ago (i'm not very good with backend and devops) and now it doesn't generate the files anymore. It tells me to use the delete command which i don't remember but even using that one my files still won't be generated. This is what my log looks like, and below you can find the code for a simple class.

One of my guesses would be this has something to do with me changing my github account, since there's the permission issue mentioned.

Im using vs code in windows 10.

log file:

E:\1 Work\flutter_pilot> flutter pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 336ms

[WARNING] Deleted previous snapshot due to missing asset graph.
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 12.5s

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

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Found 13 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository.
[SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remove them. These outputs must be removed manually or the build can be run with `--delete-conflicting-outputs`. The outputs are: lib/models/advisory-service-item.g.dart
lib/models/advisory-service.g.dart
lib/models/auth.g.dart
lib/models/contract.g.dart
lib/models/contributor.g.dart
lib/models/exact-cost-values.g.dart
lib/models/exact-cost.g.dart
lib/models/expert.g.dart
lib/models/paginator-info.g.dart
lib/models/person.g.dart
lib/models/project.g.dart
lib/models/system-country.g.dart
lib/models/user.g.dart
pub failed (78)

class file:

import 'package:json_annotation/json_annotation.dart';
part 'organisation.g.dart';

@JsonSerializable()
class Organisation {
  final String name;
  Organisation({this.name});

  factory Organisation.fromJson(Map<String, dynamic> json) => _$OrganisationFromJson(json);

  Map<String, dynamic> toJson() => _$OrganisationToJson(this);
}
morgred
  • 969
  • 5
  • 15
  • 26

19 Answers19

118

I had a similar issue, but I am using Android Studio IDE and I have done the following step:

File => Invalidate caches / Restart

Run following command:

flutter clean

flutter pub get

flutter packages pub run build_runner build --delete-conflicting-outputs  
     

About --delete-conflicting-outputs

Assume conflicting outputs in the users package are from previous builds, and skip the user prompt that would usually be provided.

Reference

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
19

If you read the error message carefully, you'll see that it's asking you to try adding --delete-conflicting-outputs at the end of your command. So you should try flutter packages pub run build_runner build --delete-conflicting-outputs.

Rick
  • 2,003
  • 4
  • 16
  • 28
  • 1
    As i already stated (mayhaps in a more broken english, since it's not my native language) i already tried that, but the files just get deleted and nothing is generated. – morgred Jan 09 '20 at 18:45
  • I see. Tho it seems the command stopped at user.g.dart? Have you checked on that file? I actually experience this error sometimes, and realize I just have a typo somewhere or a missing part 'something.g.dart' code – Rick Jan 09 '20 at 18:49
  • User is the last file in the list. Checked it it's just a name and an id get and set nothing wrong there. – morgred Jan 09 '20 at 19:15
  • I'm sorry for not being able to help you identify the problem. But based on my past experiences having that same error, it's usually a typo, some wrong code, or missing part 'file.g.dart' code. However, if you're willing to share your code with me, I can spend a bit of time to help you find where the problem is. – Rick Jan 09 '20 at 19:17
  • I Think this is a bug of build_runner delete all files related build_runner and restart the code editor , it will work – Bawantha Jan 09 '20 at 20:53
  • @Bawantha you mean remove the package and its generated files and have flutter update the packages again? – morgred Jan 10 '20 at 08:45
  • @morgred no need to remove package just delete files generated from build_runner ans start the process again – Bawantha Jan 10 '20 at 18:03
15

You just run bellow code..

flutter pub run build_runner build --delete-conflicting-outputs
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
9

In my case adding part 'file_name.g.dart'; to the top of the file helped.

file_name.dart:

import 'package:json_annotation/json_annotation.dart';

part 'file_name.g.dart';

@JsonSerializable()
class YourClass {

    factory YourClass.fromJson(Map<String, dynamic> json) => _$YourClassFromJson(json);

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

    // ... your class code
}
Sergio
  • 27,326
  • 8
  • 128
  • 149
8

I had the same issue. Solved by using the below command

flutter packages pub run build_runner build --delete-conflicting-outputs  
sourav pandit
  • 8,647
  • 2
  • 19
  • 17
Navas Koya
  • 101
  • 1
  • 3
6

I think the main reason this happens to most of us, is the possibility that when you ran this command before, you added the generated g.dart file into your source control. this in turn prevents the build_runner from regenerating the previously generated g.dart files, which it does, even if you only need the new g.dart file. now, I understand that this might be hard to follow, but consider this:

  1. Let's say you have a search_entry.dart file.
  2. You ran the command, and generated the search_entry.g.dart file successfully.
  3. You then proceed to add the generated search_entry.g.dart file to your source control repository.
  4. You make another file, let's say maybe cached_data_entry.dart.
  5. When you run the command a second time to generate the cached_data_entry.dart, it will try and delete the search_entry.g.dart file to regenerate it along with the cached_data_entry.dart file.
  6. Since the search_entry.g.dart is protected under the source control, the build runner won't be able to delete it, so, it throws this error.
  7. Now, what you need to do to fix the current situation, is delete the previously all the previously generated g.dart files, (search_entry.g.dart in our example), then add it to the ignored files in .gitignore file in your source control. then run the command again, and hopefully, you it will succeed this time.
  8. What you should do in the future, is ignore these files from your source control right after you generated them.

Now, finally, I hope I've made it as clear as it should be.

  • 2
    Since when does source control not allow editing or deleting files? –  vrnvorona Nov 30 '21 at 12:46
  • @vrnvorona I think the wording was not precise. In OP's example, since *search_entry.g.dart* is tracked into source control, a branch change or a merge will modify it, making build_runner detect an external change and consider the file as a conflicting output. This is described in the [documentation](https://pub.dev/packages/build_runner#source-control). – enzo Apr 19 '22 at 16:11
  • Anyway, this answer does a good job explaining what a conflicting output is and why does it happen. – enzo Apr 19 '22 at 16:14
5

changing the part and class to the same name of the file fixed the issue with me

7eg
  • 285
  • 3
  • 11
3

I had the same issue. At first I generated a myFirstGeneratedFile.g.dart file then I needed another object type to store then when I tried to generate the file using this command =>

flutter packages pub run build_runner clean

I got the error message.

I fixed it by deleting the file that i created before => myFirstGeneratedFile.g.dart then run the command again.

Cheers!

Imran Sefat
  • 528
  • 4
  • 6
2

Running "flutter pub upgrade" solved the problem for me.

morgred
  • 969
  • 5
  • 15
  • 26
2

try something like this via terminal:

  1. Delete previous generated files via command: find ./ -name '*.g.dart' -delete
  2. then: flutter packages pub run build_runner watch —delete-conflicting-outputs
dipnv
  • 61
  • 5
2

The issue is with package dependency, executing this flutter pub upgrade --major-versions will do the thing.

Jashan PJ
  • 4,177
  • 4
  • 27
  • 41
1

Delete .dart_tool folder and run flutter pub get. After this, run the build runner command

Iredia Ebikade
  • 1,785
  • 14
  • 18
1

In my case , I have declared a dart file named Organisation.dart instead of small letter organisation.dart , refracting this and running the build runner again worked for me.

B.shruti
  • 1,589
  • 1
  • 21
  • 41
1

in my case, I had forgotten to import the mobx_codegen package

Felipe Sales
  • 1,037
  • 1
  • 11
  • 15
1

In my case the build_runner process was successful but the .g files weren't produced. I had to restart the IDE (vscode) before I start seeing the .g files.

cksrc
  • 2,062
  • 3
  • 24
  • 39
0

Add built_value_generator to the pubspec.yaml to dev_dependencies section https://pub.dev/packages/built_value_generator

You can read the official documentation - https://dart.dev/tools/build_runner

IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16
0

Add this file in the root directory with pubspec.yaml

build.yaml file

Add the following lines

targets:
  $default:
    builders:
      freezed:
        options:
          any_map: true
          explicit_to_json: true

Then run

flutter clean
flutter pub get
flutter packages pub run build_runner build --delete-conflicting-outputs 
Job M
  • 3,331
  • 2
  • 19
  • 26
0
../../../.pub-cache/hosted/pub.dev/build_runner-2.2.0/lib/src/build_script_generate/bootstrap.dart:76:40: Error: Method not found: 'NullThrownError'.
      final error = e[0] as Object? ?? NullThrownError();

In my case, just updating all packages to the latest version solved the problem for me.

dependencies:
  freezed: ^2.4.2
  freezed_annotation: ^2.4.1
  json_serializable: ^6.7.1

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.4.6
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
Win' Stud
  • 11
  • 2
-1

Make sure this targets bit goes into build.yaml and NOT pubspec.yaml.

Nestor Ledon
  • 1,925
  • 2
  • 23
  • 36