重点:后面就用了json_serializable这个解析工具,可能看起来有些麻烦,但是也有情况会出现类型转换的问题,这时候你就要认真去对比一下服务器数据跟解析出来的数据类型了。
(1)关于JsonBeanFactory
JsonBeanFactory的可以查看这篇文章:FlutterJsonBeanFactory插件json使用
插件就是上面的这个截图
(2)json_serializable的使用
(1)yaml配置文件关联
json_serializable: ^3.0.0
build_runner: ^1.6.1
可以在https://pub.dev/packages/这个库进行搜索,使用最新的版本。
(2)打开网站,建议收藏:https://caijinglong.github.io/json2dart/index.html
上面图说了步骤:这里再说一下,选择v1.x.x,然后命名一个类,在工程中创建一个dart文件(如圈出来的3),然后把你的json放到第四个位置里,得到的右边的便是你所要的bean对象了。
(3)把bean对象张贴进去,这时候会报错,此时你不需要进行操作,只需要进行下面的第四步操作。
(4)Terminal运行:flutter packages pub run build_runner build,自动生成g.dart
在工程的Terminal下运行代码: 这是一个实时监控bean中类型变化的。主要依赖库build_runner
flutter packages pub run build_runner build
输出内容
D:\AS\Flutter>flutter packages pub run build_runner watch
[INFO] Generating build script...
[INFO] Generating build script completed, took 274ms
[INFO] Setting up file watchers...
[INFO] Setting up file watchers completed, took 8ms
[INFO] Waiting for all file watchers to be ready...
[INFO] Waiting for all file watchers to be ready completed, took 329ms
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 91ms
[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 2.1s
[INFO] Running build...
[INFO] Running build completed, took 21ms
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 47ms
[INFO] Succeeded after 73ms with 0 outputs (0 actions)
如图:
(5) 下面就看见我们生成的bean的对象,以及g.dart文件
看下g.dart文件
以上就是个人小白亲自操作教程,暂且就这么多。
————————————————
版权声明:本文为CSDN博主「xudailong_blog」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xudailong_blog/article/details/95168949
网友评论