Flutter-Json转Dart类(VS)
库引用
pubspec.yaml 文件引入
dependencies: json_annotation: <最新版本> dev_dependencies: build_runner: <最新版本> json_serializable: <最新版本>
终端运行
flutter packages get
示例
已知Json数据,例如:
{ "account": "29_0", "pwd": "29_0" }
打开 https://caijinglong.github.io/json2dart/index.html ,将Json复制进去,将右侧代码附近到新建的dart文件中,注意dart文件名
保存文件后会提出错误,因为缺少引用文件,终端输入:
flutter packages pub run build_runner build
如遇到上述命令执行错误,可运行以下命令再试
flutter packages pub run build_runner clean
flutter packages pub run build_runner build --delete-conflicting-outputs
或
flutter clean
flutter doctor
flutter packages pub run build_runner clean
flutter packages pub run build_runner build --delete-conflicting-outputs
flutter packages pub run build_runner watch
注:
关于自动化生成model,可借助JSONConverter来实现