flutter json_serializable的使用
添加依赖库:
json_serializable: ^3.3.0
build_runner: ^1.7.2
json_serializable 自动生成工具:
https://caijinglong.github.io/json2dart/index.html
生成bean文件如下步骤:
步骤1-5如图
6、创建dart文件:my_bean.dart
7、黏贴复制内容
8、studio的Terminal 运行:
(1)一次性编译: flutter packages pub run build_runner build
(2)后台自动编译: flutter packages pub run build_runner watch
9、自动生成文件:my_bean.g.dart
如果报错:pub finished with exit code 78 执行以下这句:flutter packages pub run build_runner build --delete-conflicting-outputs 再去执行编译
bean的使用:
//string 转 bean
MyBean myBean = MyBean.fromJson(json.decode("……"));
int result = myBean.result;
int seq = myBean.seq;
//bean 转 String
print("myBean = "+json.encode(myBean));