1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import 'attachment.dart';
- class Post {
- final String added;
- final List<Attachment> 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<Attachment> get mediaAttachments => files == null ? [
- ...attachments.where((a) => a.isImage || a.isVideo)
- ] : [
- ...attachments.where((a) => a.isImage || a.isVideo),files!
- ];
- factory Post.fromJson(Map<String, dynamic> 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<Attachment> _parseAttachments(
- dynamic jsonData,
- String baseUrl
- ) {
- if (jsonData is! List) return [];
-
- return jsonData
- .whereType<Map<String, dynamic>>()
- .map((x) => Attachment.fromJson(x, baseUrl))
- .whereType<Attachment>()
- .toList();
- }
- }
|