【发布时间】:2023-03-28 23:26:02
【问题描述】:
颤振版本:
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
为什么没有成功?!
【问题讨论】:
-
描述您尝试实现的目标,而不是仅发布代码,这将有助于其他人理解问题。不要试图绕过 stackoverflow 限制
-
Dart 2.0 有一些错误信息相同。 github.com/dart-lang/sdk/issues/33551
-
他正试图自动化 Json 序列化,就像我一样,并且遇到了同样的错误。没有任何意义。这就是我们关注的内容:flutter.dev/docs/development/data-and-backend/…HELP
-
您解决了吗?我有同样的问题。我想不通。
-
@bradbury9 自动 json 序列化/反序列化,查看我的其他评论
标签: flutter dart visual-studio-code