Flutter 手动json转化之后强制类型转化

GitHub地址

手动json转化之后强制类型转化

{
    "rating": {
        "max": 10,
        "average": 7.4,
        "stars": "40",
        "min": 0
    },
    "reviews_count": 301,
    "wish_count": 15912,
    "douban_site": "",
    "year": "2009",
    "images": {
        "small": "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p494268647.jpg",
        "large": "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p494268647.jpg",
        "medium": "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p494268647.jpg"
    },
    "alt": "https://movie.douban.com/subject/1764796/",
    "id": "1764796",
    "mobile_url": "https://movie.douban.com/subject/1764796/mobile",
    "title": "机器人9号",
    "do_count": null,
    "share_url": "https://m.douban.com/movie/subject/1764796",
    "seasons_count": null,
    "schedule_url": "",
    "episodes_count": null,
    "countries": [
        "美国"
    ],
    "genres": [
        "动画",
        "冒险",
        "奇幻"
    ],
    "collect_count": 74934,
    "casts": [
        {
            "alt": "https://movie.douban.com/celebrity/1054395/",
            "avatars": {
                "small": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p51597.jpg",
                "large": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p51597.jpg",
                "medium": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p51597.jpg"
            },
            "name": "伊莱贾·伍德",
            "id": "1054395"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1016673/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33305.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33305.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33305.jpg"
            },
            "name": "詹妮弗·康纳利",
            "id": "1016673"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1017907/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p55994.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p55994.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p55994.jpg"
            },
            "name": "约翰·C·赖利",
            "id": "1017907"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1036321/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p42033.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p42033.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p42033.jpg"
            },
            "name": "克里斯托弗·普卢默",
            "id": "1036321"
        }
    ],
    "current_season": null,
    "original_title": "9",
    "summary": "机器人9号(伊利亚•伍德 Elijah Wood 饰)突然醒来,发现身边的世界充满危机,四处残败,一片末世景象。9号带着一个画有三个奇怪符号的圆形物体逃到街上,幸遇发明家机器人2号(马丁•兰道 Martin Landau 饰)给自己装上了声音,但2号却不幸被机器怪兽抓走。9号找到了老兵1号(克里斯托弗•普卢默 Christopher Plummer 饰)、机械工5号(约翰•雷利 John C. Reilly 饰)、疯癫画家6号(克里斯品•格拉夫 Crispin Glover 饰)和大力士8号(弗雷德•塔塔绍尔 Fred Tatasciore 饰)。9号与5号擅自出行援救2号,危急时被女武士7号(詹妮佛•康纳利 Jennifer Connelly 饰)救下,但无意中9号却令终极机器兽复活。带着自己从哪里来以及生存使命的问题,9号决定想尽办法制服机器兽,拯救全世界……©豆瓣",
    "subtype": "movie",
    "directors": [
        {
            "alt": "https://movie.douban.com/celebrity/1276787/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1351678808.44.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1351678808.44.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1351678808.44.jpg"
            },
            "name": "申·阿克",
            "id": "1276787"
        }
    ],
    "comments_count": 9193,
    "ratings_count": 60293,
    "aka": [
        "9:末世决战",
        "九",
        "Number 9",
        "机器人9号"
    ]
}

转化为Map数组

 List<Widget> casts = snap.data['casts']
            .cast<Map<String, dynamic>>()
            .map<Widget>((item) => new Column(
                  children: <Widget>[
                    new Image.network(item['avatars']['small']),
                    new Text(item['name'])
                  ],
                ))
            .toList();
拆解步骤
1. 先进行类型转换
2. 使用map 方法遍历返回Iteration
    map<Widget>((item){
        return Widget
    }) 或者
    map<Widget>((item)=> new Widget)
    
3. 将Iteration转化为List<Widget>
    iteraInstance.toList()

转化为List<String>遍历

    snap.data['genres'].cast<String>().map<Widget>((item) {
                            return new Text(
                              ' ${item} ',
                              style: new TextStyle(
                                  color: Colors.green,
                                  fontWeight: FontWeight.w100,
                                  fontSize: 18.0),
                            );
                          }).toList()
步骤同上只是这次转为cast<String>()
    原文作者:搞好关系
    原文地址: https://www.jianshu.com/p/d7d06671f06e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞