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/