how to create a list from json string in flutter

可紊 提交于 2021-02-11 17:51:36

问题


this question is already asked in here (convert JSON String to the list object in flutter) by someone else.. but right now.. I don't get the answer yet.. so is there a way to call List<LoginRespon>... I have converted from json respond using https://app.quicktype.io/ and here is the code

import 'dart:convert';

LoginRespon loginResponFromJson(String str) => LoginRespon.fromJson(json.decode(str));

String loginResponToJson(LoginRespon data) => json.encode(data.toJson());

class LoginRespon {
    String status;
    Data data;

    LoginRespon({
        this.status,
        this.data,
    });

    factory LoginRespon.fromJson(Map<String, dynamic> json) => LoginRespon(
        status: json["status"],
        data: Data.fromJson(json["data"]),
    );

    Map<String, dynamic> toJson() => {
        "status": status,
        "data": data.toJson(),
    };
}

class Data {
    String resource;
    String unit;
    Data({
        this.resource,
        this.unit,
    });

    factory Data.fromJson(Map<String, dynamic> json) => Data(
        resource: json["resource"],
        unit: json["unit"],
    );

    Map<String, dynamic> toJson() => {
        "resource": resource,
        "unit": unit,
    };
}

and here is the original json data

{
  "status":200,
  "data":{
     "resource":"abc",
     "unit":"a"
}
}

来源:https://stackoverflow.com/questions/61554156/how-to-create-a-list-from-json-string-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!