简体   繁体   English

flutter pub run build_runner 构建失败

[英]flutter pub run build_runner build failed

flutter version: flutter 版本:

flutter_macos_v1.9.1+hotfix.2-stable

create new project in terminal:在终端中创建新项目:

flutter create myapp

open vscode, edit pubspec.yaml :打开 vscode,编辑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:新的/lib/user.dart并在下面填写:

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:在终端运行flutter pub run build_runner build

[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?!为什么从来没有成功?!

Try this.尝试这个。

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

add dependency in pubsec.yaml, analyzer: '0.39.14'在 pubsec.yaml 中添加依赖项,分析器:'0.39.14'

flutter clean
flutter pub cache repair
flutter pub run build_runner clean

Then run,然后运行,

flutter pub run build_runner build

I tried with many solutions, But error not gone.我尝试了很多解决方案,但错误并没有消失。 flutter packages pub run build_runner watch command ran with endless log. flutter packages pub run build_runner watch命令以无穷无尽的日志运行。

I deleted pubspec.lock and ran flutter pub get and installed dependency again and ran above command.我删除了pubspec.lock并再次运行flutter pub get并安装依赖项并运行上述命令。 After this error gone.这个错误消失后。

I had the same issue, so I just saved the changes first in the class (in your case User class).我遇到了同样的问题,所以我只是先将更改保存在 class (在您的情况下为User类)。
And then I just retried using:然后我只是重试使用:

flutter pub run build_runner build

Please create default empty constructor for models class before flutter packages pub run build_runner build command请在flutter packages pub run build_runner build命令之前为模型 class 创建默认的空构造函数

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经过长时间尝试上述所有建议后,我得到了最新的 build_runner 和 json_serializable 版本: build_runner: ^1.10.2 json_serializable: ^3.4.0 不确定最终是什么,但看起来像是问题之一我的案例是一个稍微过时的 dart SDK,所以还有一件事需要注意

Looks like Analyzer is breaking it, downgrading to analyzer: 0.38.2 solved it for me.看起来分析器正在破坏它,降级到analyzer: 0.38.2为我解决了它。

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

I had the same issue.我遇到过同样的问题。

Successfully generating all *.g.dart files with:成功生成所有 *.g.dart 文件:

build_runner 0.9.2 build_runner 0.9.2

json_serializable 0.5.8+1 json_serializable 0.5.8+1

json_annotation 0.2.9+1 json_annotation 0.2.9+1

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.一旦我添加了该行并再次运行该命令,它就可以正常工作了。

I had the same error.我有同样的错误。 I simply installed the build runner package in pubspec.yaml file like -我只是在 pubspec.yaml 文件中安装了构建运行程序 package,例如 -

dev_dependencies: dev_dependencies:

build_runner: ^1.3.1 build_runner:^1.3.1

mobx_codegen: ^0.3.9 mobx_codegen: ^0.3.9

you may need hive_generator in your dependencies您的依赖项中可能需要 hive_generator

Update 2020/8/24: seems to break build_runner or json_serializable in version: 2020/8/24 更新:似乎破坏了 build_runner 或 json_serializable 版本:

Analyzer: 0.39.16 

Going back to dart analyzer version:回到dart分析仪版本:

Analyzer: 0.39.14 

fixed it for me.为我修好了。 So, something is broken in 0.39.16.因此,在 0.39.16 中出现了问题。

My class name was:我的 class 名称是:

class Game

but my filename was game_model.dart So you'll need to use:但我的文件名是game_model.dart所以你需要使用:

part 'game_model.g.dart';

You may also need to add hive_generator dependency.您可能还需要添加hive_generator依赖项。

Looks like flutter clean is resolving the issue.看起来flutter clean正在解决这个问题。 Try running it before ie openApi Generator.尝试在 ie openApi Generator 之前运行它。

make sure the class name is same with the generated file name.确保 class 名称与生成的文件名相同。 For ex:例如:

part ' User .g.dart';部分'用户.g.dart';

class User { class用户{

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter 运行 pub 运行 build_runner 构建失败 - flutter run pub run build_runner build failed flutter pub run build_runner 卡住了 - flutter pub run build_runner stuck flutter pub run build_runner build --delete-conflicting-outputs 在 github 操作中运行失败 - flutter pub run build_runner build --delete-conflicting-outputs run failed in github action $ flutter pub run build_runner 在项目中构建 hive 没有响应 - $ flutter pub run build_runner build in project with hive not responding flutter pub run build_runner build 上的错误 - Error on flutter pub run build_runner build 如何修复 Flutter build_runner 错误:无法预编译 build_runner:build_runner: - How to fix Flutter build_runner error: Failed to precompile build_runner:build_runner: Flutter 无法使用 Freezed 构建 build_runner - Flutter failed to build build_runner with Freezed 如何使用 class 的“flutter pub run build_runner build”在 Flutter 中有字段时间戳? - How to “flutter pub run build_runner build” with the class has field Timestamp in Flutter? 如何在 intellij idea 中以调试模式运行 flutter 'packages pub run build_runner build'? - How run flutter 'packages pub run build_runner build' with debug mode in intellij idea? 运行时出错 flutter pub run build_runner build --delete-conflicting-outputs - Error when running flutter pub run build_runner build --delete-conflicting-outputs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM