flutter pub run build_runner 构建失败

标签 flutter dart visual-studio-code

flutter 版本:

flutter_macos_v1.9.1+hotfix.2-stable

在终端中创建新项目:

flutter create myapp

打开vscode,编辑pubspec.yaml:

dependencies:
  json_annotation: ^3.0.0

dev_dependencies:
  build_runner: ^1.7.0
  json_serializable: ^3.2.2

在终端中获取包:

flutter pub get

新建 /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);
}

在终端中运行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

为什么从来没有成功?!

最佳答案

试试这个。

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

关于flutter pub run build_runner 构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58023664/

相关文章:

dart - 查看特定时间段的问候屏幕

flutter - 如何返回StatefulWidget包含的值以在ListView.builder中使用它?

flutter - Flutter 命名路由名称重要吗?

Android Studio flutter - 警告 : Another emulator is still running - windows 10

angular - 停止 ngAfterContentInit 执行两次

javascript - 使用 VS 代码在 ASP.NET Core MVC 中调试 javascript 和 c#

visual-studio-code - 如何更改差异颜色 Visual Studio Code

android - 方法 channel 在 Flutter 模块中不起作用

dart - 如何重新抛出异常并保留堆栈跟踪?

visual-studio-code - VSCode中具有语义突出显示的语言服务器