今天看到 flutter 中 json_serializable ,看了几篇博客,暴走,本来flutter坑就不少,奈何写博客的人又坑上加坑。

NO1、

NO2、

{"curPage": 2,"datas": [{"apkLink": "","author": "鸿洋","chapterId": 410,"chapterName": "玉刚说","collect": false,"courseId": 13,"desc": "","envelopePic": "","fresh": false,"id": 7604,"link": "https://mp.weixin.qq.com/s/cCZKmqKrdCn63eWTbOuANw","niceDate": "2018-12-03","origin": "","projectLink": "","publishTime": 1543830090000,"superChapterId": 408,"superChapterName": "公众号","tags": [{"name": "公众号","url": "/wxarticle/list/410/1"}],"title": "在 Retrofit 和 OkHttp 中使用网络缓存,提高访问效率","type": 0,"userId": -1,"visible": 1,"zan": 0},{"apkLink": "","author": "鸿洋","chapterId": 408,"chapterName": "鸿洋","collect": false,"courseId": 13,"desc": "","envelopePic": "","fresh": false,"id": 7605,"link": "https://mp.weixin.qq.com/s/r3AWeYafyMEc1-g8BWEHBg","niceDate": "2018-12-03","origin": "","projectLink": "","publishTime": 1543766400000,"superChapterId": 408,"superChapterName": "公众号","tags": [{"name": "公众号","url": "/wxarticle/list/408/1"}],"title": "非 UI 线程能调用 View.invalidate()?","type": 0,"userId": -1,"visible": 1,"zan": 0}],"offset": 20,"over": false,"pageCount": 289,"size": 20,"total": 5779
}

生成

import 'package:json_annotation/json_annotation.dart'; part 'new.g.dart';@JsonSerializable()class new extends Object { // 这里的new -> Ne@JsonKey(name: 'curPage')int curPage;@JsonKey(name: 'datas')List<Datas> datas;@JsonKey(name: 'offset')int offset;@JsonKey(name: 'over')bool over;@JsonKey(name: 'pageCount')int pageCount;@JsonKey(name: 'size')int size;@JsonKey(name: 'total')int total;new(this.curPage,this.datas,this.offset,this.over,this.pageCount,this.size,this.total,); // 这里的new -> New 1处factory new.fromJson(Map<String, dynamic> srcJson) => _$newFromJson(srcJson); // 这里的new -> New 2处Map<String, dynamic> toJson() => _$newToJson(this); // 这里的new -> New 1处}@JsonSerializable()class Datas extends Object {@JsonKey(name: 'apkLink')String apkLink;@JsonKey(name: 'author')String author;@JsonKey(name: 'chapterId')int chapterId;@JsonKey(name: 'chapterName')String chapterName;@JsonKey(name: 'collect')bool collect;@JsonKey(name: 'courseId')int courseId;@JsonKey(name: 'desc')String desc;@JsonKey(name: 'envelopePic')String envelopePic;@JsonKey(name: 'fresh')bool fresh;@JsonKey(name: 'id')int id;@JsonKey(name: 'link')String link;@JsonKey(name: 'niceDate')String niceDate;@JsonKey(name: 'origin')String origin;@JsonKey(name: 'projectLink')String projectLink;@JsonKey(name: 'publishTime')int publishTime;@JsonKey(name: 'superChapterId')int superChapterId;@JsonKey(name: 'superChapterName')String superChapterName;@JsonKey(name: 'tags')List<Tags> tags;@JsonKey(name: 'title')String title;@JsonKey(name: 'type')int type;@JsonKey(name: 'userId')int userId;@JsonKey(name: 'visible')int visible;@JsonKey(name: 'zan')int zan;Datas(this.apkLink,this.author,this.chapterId,this.chapterName,this.collect,this.courseId,this.desc,this.envelopePic,this.fresh,this.id,this.link,this.niceDate,this.origin,this.projectLink,this.publishTime,this.superChapterId,this.superChapterName,this.tags,this.title,this.type,this.userId,this.visible,this.zan,);factory Datas.fromJson(Map<String, dynamic> srcJson) => _$DatasFromJson(srcJson);Map<String, dynamic> toJson() => _$DatasToJson(this);}@JsonSerializable()class Tags extends Object {@JsonKey(name: 'name')String name;@JsonKey(name: 'url')String url;Tags(this.name,this.url,);factory Tags.fromJson(Map<String, dynamic> srcJson) => _$TagsFromJson(srcJson);Map<String, dynamic> toJson() => _$TagsToJson(this);}

建文件

NO3、本人亲试


N/A: version "N/A -> N/A" is not yet installed.You need to run "nvm install N/A" to install it before using it.
N/A: version "N/A -> N/A" is not yet installed.You need to run "nvm install N/A" to install it before using it.
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 319ms[INFO] Initializing inputs
[INFO] Reading cached asset graph...You have hit a bug in build_runner
Please file an issue with reproduction steps at https://github.com/dart-lang/build/issuesNoSuchMethodError: The getter 'inputs' was called on null.
Receiver: null
Tried calling: inputs
dart:core                                                                                                            Object.noSuchMethod
../flutter/.pub-cache/hosted/pub.flutter-io.cn/build_runner_core-1.0.0/lib/src/asset_graph/serialization.dart 58:20  _AssetGraphDeserializer.deserialize
package:build_runner_core/src/asset_graph/graph.dart 42:48                                                           new AssetGraph.deserialize
package:build_runner_core/src/generate/build_definition.dart 209:38                                                  _Loader._tryReadCachedAssetGraph.<fn>
package:build_runner_core/src/logging/logging.dart 25:30                                                             logTimedAsync
package:build_runner_core/src/generate/build_definition.dart 207:12                                                  _Loader._tryReadCachedAssetGraph
package:build_runner_core/src/generate/build_definition.dart 79:28                                                   _Loader.prepareWorkspace
package:build_runner_core/src/generate/build_definition.dart 64:50                                                   BuildDefinition.prepareWorkspace
package:build_runner_core/src/generate/build_impl.dart 107:49                                                        BuildImpl.create
package:build_runner_core/src/generate/build_runner.dart 31:37                                                       BuildRunner.create
package:build_runner/src/generate/build.dart 105:35                                                                  build
package:build_runner/src/entrypoint/build.dart 28:24                                                                 BuildCommand.run
package:args/command_runner.dart 196:27                                                                              CommandRunner.runCommand
package:args/command_runner.dart 111:29                                                                              CommandRunner.run.<fn>
dart:async                                                                                                           new Future.sync
package:args/command_runner.dart 111:11                                                                              CommandRunner.run
package:build_runner/src/entrypoint/run.dart 22:31                                                                   run
.dart_tool/build/entrypoint/build.dart 18:22                                                                         mainpub finished with exit code 1
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 537ms[INFO] Initializing inputs
[INFO] Reading cached asset graph...You have hit a bug in build_runner
Please file an issue with reproduction steps at https://github.com/dart-lang/build/issuesNoSuchMethodError: The getter 'inputs' was called on null.
Receiver: null
Tried calling: inputs
dart:core                                                                                                            Object.noSuchMethod
../flutter/.pub-cache/hosted/pub.flutter-io.cn/build_runner_core-1.0.0/lib/src/asset_graph/serialization.dart 58:20  _AssetGraphDeserializer.deserialize
package:build_runner_core/src/asset_graph/graph.dart 42:48                                                           new AssetGraph.deserialize
package:build_runner_core/src/generate/build_definition.dart 209:38                                                  _Loader._tryReadCachedAssetGraph.<fn>
package:build_runner_core/src/logging/logging.dart 25:30                                                             logTimedAsync
package:build_runner_core/src/generate/build_definition.dart 207:12                                                  _Loader._tryReadCachedAssetGraph
package:build_runner_core/src/generate/build_definition.dart 79:28                                                   _Loader.prepareWorkspace
package:build_runner_core/src/generate/build_definition.dart 64:50                                                   BuildDefinition.prepareWorkspace
package:build_runner_core/src/generate/build_impl.dart 107:49                                                        BuildImpl.create
package:build_runner_core/src/generate/build_runner.dart 31:37                                                       BuildRunner.create
package:build_runner/src/generate/build.dart 105:35                                                                  build
package:build_runner/src/entrypoint/build.dart 28:24                                                                 BuildCommand.run
package:args/command_runner.dart 196:27                                                                              CommandRunner.runCommand
package:args/command_runner.dart 111:29                                                                              CommandRunner.run.<fn>
dart:async                                                                                                           new Future.sync
package:args/command_runner.dart 111:11                                                                              CommandRunner.run
package:build_runner/src/entrypoint/run.dart 22:31                                                                   run
.dart_tool/build/entrypoint/build.dart 18:22                                                                         mainpub finished with exit code 1
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 496ms[INFO] Initializing inputs
[INFO] Reading cached asset graph...You have hit a bug in build_runner
Please file an issue with reproduction steps at https://github.com/dart-lang/build/issuesNoSuchMethodError: The getter 'inputs' was called on null.
Receiver: null
Tried calling: inputs
dart:core                                                                                                            Object.noSuchMethod
../flutter/.pub-cache/hosted/pub.flutter-io.cn/build_runner_core-1.0.0/lib/src/asset_graph/serialization.dart 58:20  _AssetGraphDeserializer.deserialize
package:build_runner_core/src/asset_graph/graph.dart 42:48                                                           new AssetGraph.deserialize
package:build_runner_core/src/generate/build_definition.dart 209:38                                                  _Loader._tryReadCachedAssetGraph.<fn>
package:build_runner_core/src/logging/logging.dart 25:30                                                             logTimedAsync
package:build_runner_core/src/generate/build_definition.dart 207:12                                                  _Loader._tryReadCachedAssetGraph
package:build_runner_core/src/generate/build_definition.dart 79:28                                                   _Loader.prepareWorkspace
package:build_runner_core/src/generate/build_definition.dart 64:50                                                   BuildDefinition.prepareWorkspace
package:build_runner_core/src/generate/build_impl.dart 107:49                                                        BuildImpl.create
package:build_runner_core/src/generate/build_runner.dart 31:37                                                       BuildRunner.create
package:build_runner/src/generate/build.dart 105:35                                                                  build
package:build_runner/src/entrypoint/build.dart 28:24                                                                 BuildCommand.run
package:args/command_runner.dart 196:27                                                                              CommandRunner.runCommand
package:args/command_runner.dart 111:29                                                                              CommandRunner.run.<fn>
dart:async                                                                                                           new Future.sync
package:args/command_runner.dart 111:11                                                                              CommandRunner.run
package:build_runner/src/entrypoint/run.dart 22:31                                                                   run
.dart_tool/build/entrypoint/build.dart 18:22                                                                         mainpub finished with exit code 1
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 448ms[INFO] Initializing inputs
[INFO] Reading cached asset graph...You have hit a bug in build_runner
Please file an issue with reproduction steps at https://github.com/dart-lang/build/issuesNoSuchMethodError: The getter 'inputs' was called on null.
Receiver: null
Tried calling: inputs
dart:core                                                                                                            Object.noSuchMethod
../flutter/.pub-cache/hosted/pub.flutter-io.cn/build_runner_core-1.0.0/lib/src/asset_graph/serialization.dart 58:20  _AssetGraphDeserializer.deserialize
package:build_runner_core/src/asset_graph/graph.dart 42:48                                                           new AssetGraph.deserialize
package:build_runner_core/src/generate/build_definition.dart 209:38                                                  _Loader._tryReadCachedAssetGraph.<fn>
package:build_runner_core/src/logging/logging.dart 25:30                                                             logTimedAsync
package:build_runner_core/src/generate/build_definition.dart 207:12                                                  _Loader._tryReadCachedAssetGraph
package:build_runner_core/src/generate/build_definition.dart 79:28                                                   _Loader.prepareWorkspace
package:build_runner_core/src/generate/build_definition.dart 64:50                                                   BuildDefinition.prepareWorkspace
package:build_runner_core/src/generate/build_impl.dart 107:49                                                        BuildImpl.create
package:build_runner_core/src/generate/build_runner.dart 31:37                                                       BuildRunner.create
package:build_runner/src/generate/build.dart 105:35                                                                  build
package:build_runner/src/entrypoint/build.dart 28:24                                                                 BuildCommand.run
package:args/command_runner.dart 196:27                                                                              CommandRunner.runCommand
package:args/command_runner.dart 111:29                                                                              CommandRunner.run.<fn>
dart:async                                                                                                           new Future.sync
package:args/command_runner.dart 111:11                                                                              CommandRunner.run
package:build_runner/src/entrypoint/run.dart 22:31                                                                   run
.dart_tool/build/entrypoint/build.dart 18:22                                                                         mainpub finished with exit code 1
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$  flutter packages pub run build_runner clean
[INFO] Generating build script...
[INFO] Generating build script completed, took 329ms[WARNING] Deleting cache and generated source files.
This shouldn't be necessary for most applications, unless you have made intentional edits to generated files (i.e. for testing). Consider filing a bug at https://github.com/dart-lang/build/issues/new if you are using this to work around an apparent (and reproducible) bug.
[INFO] Cleaning up source outputs...
[WARNING] Failed to deserialize AssetGraph. Skipping cleanup of generated files in source directories.
[INFO] Cleaning up source outputs completed, took 142ms[INFO] Cleaning up cache directory...
[INFO] Cleaning up cache directory completed, took 22mschenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 317ms[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 2.2s[INFO] Checking for unexpected pre-existing outputs....
[INFO] Found 1 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository.
[SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remove them. These outputs must be removed manually or the build can be run with `--delete-conflicting-outputs`. The outputs are: lib/data.g.dart
pub finished with exit code 78
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build --delete-conflicting-output
Could not find an option named "delete-conflicting-output".Usage: build_runner build [arguments] [directories]
-h, --help                          Print this usage information.--[no-]assume-tty               Enables colors and interactive input when the script does not appear to be running directly in a terminal, for instance when it is a subprocess--delete-conflicting-outputs    By default, the user will be prompted to delete any files which already exist but were not known to be generated by this specific build script.Enabling this option skips the prompt and deletes the files. This should typically be used in continues integration servers and tests, but not otherwise.--low-resources-mode            Reduce the amount of memory consumed by the build process. This will slow down builds but allow them to progress in resource constrained environments.
-c, --config                        Read `build.<name>.yaml` instead of the default `build.yaml`--[no-]track-performance        Enables performance tracking and the /$perf page.--log-performance               A directory to write performance logs to, must be in the current package. Implies `--track-performance`.
-o, --output                        A directory to copy the fully built package to. Or a mapping from a top-level directory in the package to the directory to write a filtered build output to. For example "web:deploy".
-v, --verbose                       Enables verbose logging.
-r, --[no-]release                  Build with release mode defaults for builders.--define                        Sets the global `options` config for a builder by key.--[no-]symlink                  Symlink files in the output directories, instead of copying.Run "build_runner help" to see global options.
pub finished with exit code 64
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner build --delete-conflicting-output
Could not find an option named "delete-conflicting-output".Usage: build_runner build [arguments] [directories]
-h, --help                          Print this usage information.--[no-]assume-tty               Enables colors and interactive input when the script does not appear to be running directly in a terminal, for instance when it is a subprocess--delete-conflicting-outputs    By default, the user will be prompted to delete any files which already exist but were not known to be generated by this specific build script.Enabling this option skips the prompt and deletes the files. This should typically be used in continues integration servers and tests, but not otherwise.--low-resources-mode            Reduce the amount of memory consumed by the build process. This will slow down builds but allow them to progress in resource constrained environments.
-c, --config                        Read `build.<name>.yaml` instead of the default `build.yaml`--[no-]track-performance        Enables performance tracking and the /$perf page.--log-performance               A directory to write performance logs to, must be in the current package. Implies `--track-performance`.
-o, --output                        A directory to copy the fully built package to. Or a mapping from a top-level directory in the package to the directory to write a filtered build output to. For example "web:deploy".
-v, --verbose                       Enables verbose logging.
-r, --[no-]release                  Build with release mode defaults for builders.--define                        Sets the global `options` config for a builder by key.--[no-]symlink                  Symlink files in the output directories, instead of copying.Run "build_runner help" to see global options.
pub finished with exit code 64
chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner clean
[INFO] Generating build script...
[INFO] Generating build script completed, took 488ms[WARNING] Deleting cache and generated source files.
This shouldn't be necessary for most applications, unless you have made intentional edits to generated files (i.e. for testing). Consider filing a bug at https://github.com/dart-lang/build/issues/new if you are using this to work around an apparent (and reproducible) bug.
[INFO] Cleaning up source outputs...
[WARNING] No asset graph found. Skipping cleanup of generated files in source directories.
[INFO] Cleaning up source outputs completed, took 20ms[INFO] Cleaning up cache directory...
[INFO] Cleaning up cache directory completed, took 4mschenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner  build --delete-conflicting-outputs
[INFO] Generating build script...
[INFO] Generating build script completed, took 577ms[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 691ms[INFO] Checking for unexpected pre-existing outputs....
[INFO] Deleting 1 declared outputs which already existed on disk.
[INFO] Checking for unexpected pre-existing outputs. completed, took 4ms[INFO] Running build...
[INFO] 1.1s elapsed, 0/16 actions completed.
[INFO] 2.1s elapsed, 0/16 actions completed.
[INFO] 3.2s elapsed, 0/16 actions completed.
[INFO] 11.1s elapsed, 15/16 actions completed.
[INFO] 12.3s elapsed, 16/16 actions completed.
[INFO] Running build completed, took 13.2s[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 83ms[INFO] Succeeded after 13.3s with 2 outputs (101 actions)chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$ flutter packages pub run build_runner  build --delete-conflicting-outputs
[INFO] Generating build script...
[INFO] Generating build script completed, took 378ms[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 141ms[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 772ms[INFO] Running build...
[INFO] Running build completed, took 751ms[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 45ms[INFO] Succeeded after 806ms with 2 outputs (3 actions)chenhailong@chenhailongdeMacBook-Pro:~/development/my_app$

生成文件后,需等待项目文件报不红时才可以

警言: 看博客要擦亮你的双眼,动动你的小手,不要相信任何人哦

json_serializable相关推荐

  1. 使用 json_serializable (flutter packages pub run build_runner build) 问题

    命令: flutter packages pub run build_runner build 使用 build_runner 生成 .g.dart 文件 flutter packages pub r ...

  2. Flutter开发之JSON及序列化(29)

    本文介绍了使用JSON的两个常规策略: 手动序列化和反序列化 通过代码生成自动序列化和反序列化 不同的项目具有不同的复杂度和场景.对于较小项目,使用代码生成器可能会过度.对于具有多个JSON mode ...

  3. 【Flutter】JSON 模型转换 ( JSON 序列化工具 | JSON 手动序列化 | 根据 JSON 编写 Dart 模型类 | 在线自动根据 JSON 转换 Dart 类 )

    文章目录 一.JSON 序列化工具 二.JSON 手动序列化 三.根据 JSON 编写 Dart 模型类 四.在线自动转换 五.相关资源 一.JSON 序列化工具 JSON 格式比较简单的话 , 使用 ...

  4. Flutter 入门指北(Part 13)之网络

    该文已授权公众号 「码个蛋」,转载请指明出处 前面讲完了常用的部件,BLoC 模式,数据持久化等常用的,今天再介绍个重头戏 -- 网络请求 HttpClient HttpClient 是 dart 自 ...

  5. 在Flutter中解析复杂的JSON(一篇顶十篇)

    文章目录 JSON结构#1:简单 map 访问对象 Snippet #1 : imports **Snippet #2 : **加载Json Asset(可选)** Snippet #3 : 加载响应 ...

  6. 如何在Flutter上优雅地序列化一个对象

    序列化一个对象才是正经事 对象的序列化和反序列化是我们日常编码中一个非常基础的需求,尤其是对一个对象的json encode/decode操作.每一个平台都会有相关的库来帮助开发者方便得进行这两个操作 ...

  7. c语言josn序列化和反序列化,Flutter 中 JSON 的序列化和反序列化

    前言 Flutter 中没有类似于 Java 中 Gson/Jackson 这样的 JSON 序列化库. 因为这些库都是通过反射实现的,而 Flutter 中不支持反射. 其实 Dart 是支持反射的 ...

  8. Flutter社区和资源传送门

    新: 慕课网<Flutter入门与案例实战>   |   中文网<Flutter实战>电子书 字体图标生成 http://fluttericon.com/ Flutter中文网 ...

  9. Flutter 升级 2.0 填坑指导,带你原地起飞

    作者 | 恋猫de小郭       责编 | 欧阳姝黎 Flutter 2.0 的发布带来了很多的 break change ,特别是新增加的空声明安全,「相信不少大哥尝鲜之后立马反思自己" ...

最新文章

  1. python处理xml中非法字符的一种思路
  2. boost库在工作(20)线程之五
  3. attention mechanis 的数学理解
  4. Python中的注释和算数运算符
  5. Django从理论到实战(part45)--下载CSV文件
  6. 感谢贫穷!707分考入北大,她的这篇文章看哭所有人…
  7. python高阶函数map_简单了解python高阶函数map/reduce
  8. WIFI 网络操作--------------------笔记
  9. idea视图化配置html页面,IntelliJ IDEA:配置JavaScript库
  10. excel冻结窗口_excel中使用快捷方式锁定单元格,冻结某些单元格、公式或单元格...
  11. Android使用Volley框架显示“Cleartext HTTP traffic to XXX not permitted”的解决方法
  12. 微信公众号常见问题:
  13. matlab儒略日转日期,将日期(年、月、日)转换为儒略日编号并返回日期
  14. MCE公司:新型Kappa阿片受体拮抗剂和偏向性激动剂的发现
  15. 苹果计算机安装应用软件,Mac苹果电脑怎么安装软件 Mac苹果电脑安装软件新手教程...
  16. 计算机一级wpsoffice知识点,计算机一级考试WPSOffice考试大纲
  17. 洛谷 5061 秘密任务——二分图染色
  18. JavaWeb-WEB核心7 会话技术 理解什么是会话跟踪技术掌握Cookie的使用掌握Session的使用(钝化、活化)完善用户登录注册案例的功能
  19. 每日新闻丨五方面推动云计算产业发展;字节跳动与澎湃合资成立视听科技
  20. github 遇到的问题

热门文章

  1. python 两个队列进行对比
  2. “北漂青年”马云建言:未来服务业发展有三大关键词
  3. HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)
  4. 大数据的5个“小观点 ”(转)
  5. Codeforces Gym 100187D D. Holidays 排列组合
  6. GitHub for windows使用备忘录
  7. SharePoint Windows Workflow Foundation
  8. 016、JVM实战总结:大厂面试题:JVM中有哪些垃圾回收算法,每个算法各自的优劣?
  9. Kafka-batch.size属性
  10. 计算机网络自顶向下-运输层