出现了一个报错 关于json_serializable的

来源:3-10 三种JSON解析技巧带你解放生产力

weixin_慕神9503298

2021-03-17

你好
我根据视频的步骤 创建了result.dart

import 'package:json_annotation/json_annotation.dart';

part 'result.g.dart';

///这个标注是告诉生成器,这个类是需要生成Model类的
@JsonSerializable()
class Result {
  final int code;
  final String method;
  final String requestPrams;

  Result({this.code, this.method, this.requestPrams});
  factory Result.fromJson(Map<String, dynamic> json) => _$ResultFromJson(json);
  Map<String, dynamic> toJson() => _$ResultToJson(this);
}

运行命令

flutter packages pub run build_runner build

然后出现以下的错误

[INFO] Generating build script...
[INFO] Generating build script completed, took 470ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 60ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 469ms

[INFO] Running build...
[SEVERE] json_serializable:json_serializable on lib/model/result.dart:

Generator cannot target libraries that have not been migrated to null-safety.
package:flutter_bili_app/model/result.dart:8:7
  ╷
8 │ class Result {
  │       ^^^^^^
  ╵
[INFO] Running build completed, took 965ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 43ms

[SEVERE] Failed after 1.0s
pub finished with exit code 1

flutter的环境变量相关的参数
运行flutter doctor
结果如下

[√] Flutter (Channel stable, 2.0.0, on Microsoft Windows [Version 10.0.18363.1139], locale zh-CN)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 3.6)
[√] IntelliJ IDEA Ultimate Edition (version 2019.3)
[√] VS Code (version 1.54.3)
[√] Connected device (2 available)

如有回答 感激不尽

写回答

1回答

CrazyCodeBoy

2021-03-18

flutter版本和序列化插件版本不对应导致的,看下这个解决方案:https://stackoverflow.com/questions/66477445/generator-cannot-target-libraries-that-have-not-been-migrated-to-null-safety

1
1
weixin_慕神9503298
好的 谢谢你的回答
2021-03-23
共1条回复

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1723 学习 · 870 问题

查看课程