记一次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"],
);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术