flutter使用json

作者: pengshuangta | 来源:发表于2019-07-02 15:55 被阅读0次
一、先上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
image.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
image.png
  • 使用model。


    image.png
三、参考链接:

参考一
参考二

相关文章

网友评论

    本文标题:flutter使用json

    本文链接:https://www.haomeiwen.com/subject/pnzzcctx.html