简体   繁体   中英

Why doesn't build_runner generate files when serializing JSON in dart/flutter

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);
}

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  

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 .

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
}

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.

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

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!

你只需运行下面的代码..

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

运行“flutter pub upgrade”为我解决了这个问题。

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

就我而言,我已经声明了一个名为Organisation.dart的 dart 文件,而不是小写的organisation.dart ,折射这个并再次运行构建运行器对我有用。

就我而言,我忘记导入 mobx_codegen

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

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

Add built_value_generator to the pubspec.yaml<\/code> to dev_dependencies<\/code> section https:\/\/pub.dev\/packages\/built_value_generator<\/a>

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.

"

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM