首先在pubspec.yaml
中添加依賴庫:
json_serializable
json_annotation
然后打開網(wǎng)址:https://caijinglong.github.io/json2dart/index.html 輸入json字符串和你需要的類名,然后下載生成好的dart文件。
生成dart文件.png
這時(shí)候你將生成的文件放到項(xiàng)目里面之后會(huì)報(bào)錯(cuò)椭符,提示缺少對應(yīng)的.g.dart
文件
接下來在Terminal中輸入以下命令自動(dòng)生成.g.dart
文件
flutter packages pub run build_runner build --delete-conflicting-outputs
.g.dart生成成功.png