skywood/lib/utils/models/drama/skywood_home.dart

47 lines
1.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:skywood/utils/models/drama/skywood_drama.dart';
class SkywoodHome {
final String? moduleKey;
final List<SkywoodDrama> dataList;
const SkywoodHome({required this.moduleKey, required this.dataList});
factory SkywoodHome.fromJson(Map<String, dynamic> json) {
// Map<String, dynamic>? map = json['data'];
// List? list = map!['list'];
// List<SkywoodDrama> datas = [];
// if (list != null) {
// datas = list.map((e) => SkywoodDrama.fromJson(e)).toList();
// }
final data = json['data'];
List<SkywoodDrama> videos = [];
// 🔥 判断 data 的类型
if (data is Map<String, dynamic>) {
// 情况1: data 是 Map从中取 list
final list = data['list'];
if (list is List) {
videos = list.map((e) => SkywoodDrama.fromJson(e)).toList();
}
} else if (data is List) {
// 情况2: data 直接就是 List
videos = data.map((e) => SkywoodDrama.fromJson(e)).toList();
}
return SkywoodHome(
moduleKey: json['module_key'] as String?,
dataList: videos,
);
}
Map<String, dynamic> toJson() {
return {'module_key': moduleKey, 'dataList': dataList};
}
SkywoodHome copyWith({String? moduleKey, dynamic data}) {
return SkywoodHome(
moduleKey: moduleKey ?? this.moduleKey,
dataList: data ?? dataList,
);
}
}