我在使用build_runner为json文件获得所需输出时遇到问题,该文件成功运行,但没有给出任何输出
我的第一个.dart文件
```
import 'package:json_annotation/json_annotation.dart';
part 'first.g.dart';
@JsonSerializable()
class User {
String visibility, name;
User(this.visibility, this.name);
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
```
我的项目中是否需要任何buil.yaml文件?
我的pubspec.yaml文件
```
dependencies:
flutter:
sdk: flutter
json_annotation: ^4.4.0
http: ^0.13.4
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.1.7
json_serializable: ^6.1.1
```
这就是我的终端在执行命令后的样子
flutter pub run build_runner build
终端
PS D:Flutterjson_parselearn> flutter pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 359ms
[INFO] Precompiling build script......
[INFO] Precompiling build script... completed, took 7.0s
[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 758ms
[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Running build...
[INFO] Generating SDK summary...
[INFO] 3.5s elapsed, 0/3 actions completed.
[INFO] Generating SDK summary completed, took 3.5s
[INFO] 4.6s elapsed, 0/3 actions completed.
[INFO] 5.6s elapsed, 0/3 actions completed.
[INFO] 7.0s elapsed, 0/3 actions completed.
[INFO] 12.9s elapsed, 1/3 actions completed.
[INFO] Running build completed, took 13.6s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 30ms
[INFO] Succeeded after 13.6s with 0 outputs (6 actions)
我不知道我的代码有什么问题
我以前遇到过这个问题。我观察到,如果在运行build_runner之前没有保存代码,那么build_runne输出会给我们0。因此,我建议
"在运行buildrunner之前,请考虑保存对数据类的更改">
通过解决
flutter clean
flutter packages pub run build_runner watch --delete-conflicting-outputs
将其添加到pubspec.ymal文件中
dependencies:
flutter:
sdk: flutter
hive: ^2.2.1
path_provider: ^2.0.10
json_annotation: ^3.0.0
hive_flutter: ^1.0.0
hive_generator: ^1.0.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.3.1
json_serializable: ^3.2.2
添加"hive_generator:^2.0.0">和";build_runner:^2.3.2">到"strong>";dev_dependences:"部分;pubspec.yaml";文件
然后在终端中使用此命令:flutter包pub-runner watch-使用轮询watch-删除冲突输出
值得注意的是检查您的pubspec.yaml空间。我查了一下,发现有些是在片场。在中更正它们后,它就可以工作了