47 lines
1.5 KiB
Dart
47 lines
1.5 KiB
Dart
class DiscountEntity {
|
|
final String id;
|
|
final String name;
|
|
final String shopName;
|
|
final List<String> images;
|
|
final String type;
|
|
final String description;
|
|
final double price;
|
|
final double nPrice;
|
|
final DateTime? endDate;
|
|
|
|
DiscountEntity({
|
|
required this.id,
|
|
required this.name,
|
|
required this.shopName,
|
|
required this.images,
|
|
required this.type,
|
|
required this.description,
|
|
required this.price,
|
|
required this.nPrice,
|
|
required this.endDate,
|
|
});
|
|
|
|
factory DiscountEntity.fromJson(Map<String, dynamic> json) {
|
|
// A helper function to safely extract lists
|
|
List<String> _parseImages(dynamic imageList) {
|
|
if (imageList is List) {
|
|
return imageList.map((img) => (img['Url'] ?? '') as String).toList();
|
|
}
|
|
return [];
|
|
}
|
|
|
|
return DiscountEntity(
|
|
id: json['_id'] ?? '',
|
|
name: json['Name'] ?? 'بدون نام',
|
|
// Safely access nested properties
|
|
shopName: (json['Shop'] != null ? json['Shop']['Name'] : 'فروشگاه') ?? 'فروشگاه',
|
|
images: _parseImages(json['Images']),
|
|
type: (json['Type'] != null ? json['Type']['Description'] : 'عمومی') ?? 'عمومی',
|
|
description: json['Description'] ?? '',
|
|
price: (json['Price'] as num? ?? 0).toDouble(),
|
|
nPrice: (json['NPrice'] as num? ?? 0).toDouble(),
|
|
// Handle potential null or invalid date
|
|
endDate: json['EndDate'] != null ? DateTime.tryParse(json['EndDate']) : null,
|
|
);
|
|
}
|
|
} |