class TranslationModel { final int id; final String locale; TranslationModel({required this.id, required this.locale}); Map toJson() { return {'id': id, 'language': locale}; } factory TranslationModel.fromJson(Map json) { return TranslationModel(id: json['id'] ?? 0, locale: json['locale'] ?? ''); } } class Files { final String? youtube; final String? video; final String? audio; final String? booklet; final String? simple; Files({this.youtube, this.video, this.audio, this.booklet, this.simple}); Map toJson() { return { 'youtube': youtube, 'video': video, 'audio': audio, 'booklet': booklet, 'simple': simple, }; } factory Files.fromJson(Map json) { return Files( youtube: json['youtube'] as String?, video: json['video'] as String?, audio: json['audio'] as String?, booklet: json['booklet'] as String?, simple: json['simple'] as String?, ); } } class DurationField { final int duration; DurationField({required this.duration}); factory DurationField.fromJson(Map json) { // convert duration from string to int final dynamic rawDuration = json['duration']; final int parsedDuration = rawDuration is String ? int.tryParse(rawDuration) ?? 0 : rawDuration is int ? rawDuration : 0; return DurationField(duration: parsedDuration); } } class ItemModel { final String id; final String title; final DateTime date; final String type; final String? slug; bool rm; String biblestudy; bool draft; int timestamp; String activity; DurationField duration; String place; String city; String state; String country; // List type; String thumbnail; List translations; Files files; ItemModel({ required this.id, required this.title, required this.date, this.type = '', this.slug, this.rm = false, this.biblestudy = '', this.draft = false, this.timestamp = 0, this.activity = '', required this.duration, this.place = '', this.city = '', this.state = '', this.country = '', // this.type = const [], this.thumbnail = '', this.translations = const [], Files? files, }) : files = files ?? Files(); Map toJson() { return { 'id': id, 'title': title, 'date': date.toIso8601String(), 'type': type, 'slug': slug, 'rm': rm, 'biblestudy': biblestudy, 'draft': draft, 'timestamp': timestamp, 'activity': activity, 'duration': duration, 'place': place, 'city': city, 'state': state, 'country': country, 'thumbnail': thumbnail.toString(), 'translations': translations.toString(), 'files': files.toString(), }; } factory ItemModel.fromJson(Map json) { return ItemModel( id: json['id'] ?? '', title: json['title'] ?? '', date: json['date'] != null ? DateTime.parse(json['date'].toString()) : DateTime.now(), type: json['type'] ?? '', slug: json['slug'], rm: json['rm'] ?? false, biblestudy: json['biblestudy'] ?? '', draft: json['draft'] ?? false, timestamp: json['timestamp'] ?? 0, activity: json['activity'] ?? '', duration: DurationField.fromJson({'duration': json['duration']}), place: json['place'] ?? '', city: json['city'] ?? '', state: json['state'] ?? '', country: json['country'] ?? '', // type: List.from(json['type'] ?? []), thumbnail: json['thumbnail'] ?? '', translations: (json['translations'] as List?) ?.map( (item) => TranslationModel.fromJson(item as Map), ) .toList() ?? [], files: json['files'] != null ? (json['files'] is Map ? Files.fromJson(json['files'] as Map) : Files()) : Files(), ); } } class ResultModel { final List years; ResultModel({required this.years}); factory ResultModel.fromJson(Map json) { return ResultModel( years: (json['years'] as List) .map((e) => YearModel.fromJson(e)) .toList(), ); } } class YearModel { final int year; final int count; final List months; YearModel({required this.year, required this.count, required this.months}); factory YearModel.fromJson(Map json) { return YearModel( year: json['year'], count: json['count'], months: (json['months'] as List) .map((e) => MonthModel.fromJson(e)) .toList(), ); } } class MonthModel { final String name; final String month; final int count; MonthModel({required this.name, required this.month, required this.count}); factory MonthModel.fromJson(Map json) { return MonthModel( name: json['name'], month: json['month'], count: int.parse(json['count']), ); } }