import 'attachment.dart'; class Post { final String added; final List attachments; final Attachment? files; final String content; final String edited; final String id; final String published; final String service; final String title; final String user; final String baseUrl; Post({ required this.added, required this.attachments, required this.files, required this.content, required this.edited, required this.id, required this.published, required this.service, required this.title, required this.user, required this.baseUrl, }); List get mediaAttachments => files == null ? [ ...attachments.where((a) => a.isImage || a.isVideo) ] : [ ...attachments.where((a) => a.isImage || a.isVideo),files! ]; factory Post.fromJson(Map json, String baseUrl) => Post( added: _parseString(json['added']), attachments: _parseAttachments(json['attachments'], baseUrl), files: (json["file"] as Map)?.isNotEmpty == true ? Attachment.fromJson(json["file"], baseUrl) : null, content: _parseString(json['substring']), edited: _parseString(json['edited']), id: _parseString(json['id']), published: _parseString(json['published']), service: _parseString(json['service']), title: _parseString(json['title']), user: _parseString(json['user']), baseUrl: baseUrl, ); static String _parseString(dynamic value) => (value is String) ? value : ''; static List _parseAttachments( dynamic jsonData, String baseUrl ) { if (jsonData is! List) return []; return jsonData .whereType>() .map((x) => Attachment.fromJson(x, baseUrl)) .whereType() .toList(); } }