Loading

记一次Flutter Json数组转换为List对象

在dio请求数据之后进行fromJson操作

// response是请求接口后返回的json数据,调用fromJson方法
DevicePageListResponseEntity.fromJson(response);

// 实体类
class DevicePageListResponseEntity {
  int? code;
  List<DeviceItem>? items;

  DevicePageListResponseEntity({
    this.code,
    this.items,
  });

  factory DevicePageListResponseEntity.fromJson(Map<String, dynamic> json) =>
      DevicePageListResponseEntity(
          code: json["code"],
          items: json["data"]["items"] == null
              ? []
              : List<DeviceChannel>.from(
                  json["data"]["items"].map((x) => DeviceChannel.fromJson(x))));
}

class DeviceChannel {
  int? channelId;
  int? byEnable;
  String? rtspStream;

  DeviceChannel({
    this.channelId,
    this.byEnable,
    this.rtspStream,
  });

  Map<String, dynamic> toJson() => {
        "channelId": channelId,
        "byEnable": byEnable,
        "rtspStream": rtspStream,
      };

  factory DeviceChannel.fromJson(Map<String, dynamic> json) => DeviceChannel(
        channelId: json["channelId"],
        byEnable: json["byEnable"],
        rtspStream: json["rtspStream"],
      );
}
posted @ 2022-09-16 22:08  老卫同学  阅读(303)  评论(0)    收藏  举报