flutter使用json
2019-07-02 本文已影响0人
pengshuangta
一、先上demo: github_json_demo
二、json快速转模型的步骤:
- 添加如下三个库,注:build_runner 和 json_serializable的光标缩进,要跟flutter_test在同一层级;
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
json_annotation: ^2.4.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.6.0
json_serializable: ^3.0.0
![](https://img.haomeiwen.com/i6096855/1b3483a962cf53ad.png)
- 打开链接复制你的json数据,然后快速生产xxx_model.dart文件,下载改dart文件;
image.png
- 将下载好的xxx_model.dart文件,拖入工程,终端cd到项目的根目录下,执行如下命令,会自动生成xxx_model.g.dart文件。
# 命令的作用是监听生成xxx_model.g.dart文件,每次文件更改,会自动生成;
flutter packages pub run build_runner watch
![](https://img.haomeiwen.com/i6096855/792e9a4164c8021a.png)
-
使用model。
image.png