47 lines
1.3 KiB
Dart
47 lines
1.3 KiB
Dart
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,
|
||
);
|
||
}
|
||
}
|