[英]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.我遇到过同样的问题。
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.