100 lines
2.8 KiB
Dart
100 lines
2.8 KiB
Dart
class CargoItem {
|
||
final String id; // Will store the timestamp as string
|
||
final String jobId; // Will store the timestamp as string
|
||
final String description;
|
||
final int quantity;
|
||
final double weightKg;
|
||
final double lengthCm;
|
||
final double widthCm;
|
||
final double heightCm;
|
||
|
||
CargoItem({
|
||
required this.id,
|
||
required this.jobId,
|
||
required this.description,
|
||
required this.quantity,
|
||
required this.weightKg,
|
||
required this.lengthCm,
|
||
required this.widthCm,
|
||
required this.heightCm,
|
||
});
|
||
|
||
static String _readString(dynamic value, {String fallback = ''}) {
|
||
if (value is String) {
|
||
return value;
|
||
}
|
||
if (value is num || value is bool) {
|
||
return value.toString();
|
||
}
|
||
return fallback;
|
||
}
|
||
|
||
factory CargoItem.fromJson(Map<String, dynamic> json) {
|
||
// Parse the complex id object - can be either a Map or a simple string
|
||
String idValue = '';
|
||
if (json['id'] is Map) {
|
||
final idMap = json['id'] as Map<String, dynamic>;
|
||
idValue = idMap['timestamp']?.toString() ?? '';
|
||
} else {
|
||
idValue = json['id']?.toString() ?? '';
|
||
}
|
||
|
||
// Parse the complex jobId object - can be either a Map or a simple string
|
||
String jobIdValue = '';
|
||
if (json['jobId'] is Map) {
|
||
final jobIdMap = json['jobId'] as Map<String, dynamic>;
|
||
jobIdValue = jobIdMap['timestamp']?.toString() ?? '';
|
||
} else {
|
||
jobIdValue = json['jobId']?.toString() ?? '';
|
||
}
|
||
|
||
return CargoItem(
|
||
id: idValue,
|
||
jobId: jobIdValue,
|
||
description: _readString(json['description']),
|
||
quantity: json['quantity'] is num ? json['quantity'].toInt() : 0,
|
||
weightKg: json['weightKg'] is num ? json['weightKg'].toDouble() : 0.0,
|
||
lengthCm: json['lengthMm'] is num ? json['lengthMm'].toDouble() : 0.0,
|
||
widthCm: json['widthMm'] is num ? json['widthMm'].toDouble() : 0.0,
|
||
heightCm: json['heightMm'] is num ? json['heightMm'].toDouble() : 0.0,
|
||
);
|
||
}
|
||
|
||
Map<String, dynamic> toJson() {
|
||
return {
|
||
'id': id,
|
||
'jobId': jobId,
|
||
'description': description,
|
||
'quantity': quantity,
|
||
'weightKg': weightKg,
|
||
'lengthMm': lengthCm,
|
||
'widthMm': widthCm,
|
||
'heightMm': heightCm,
|
||
};
|
||
}
|
||
|
||
@override
|
||
String toString() {
|
||
return 'CargoItem(id: $id, description: $description, quantity: $quantity)';
|
||
}
|
||
|
||
@override
|
||
bool operator ==(Object other) {
|
||
if (identical(this, other)) return true;
|
||
return other is CargoItem && other.id == id;
|
||
}
|
||
|
||
@override
|
||
int get hashCode => id.hashCode;
|
||
|
||
/// Get formatted dimensions string for display
|
||
String get formattedDimensions {
|
||
return '${lengthCm.toInt()} × ${widthCm.toInt()} × ${heightCm.toInt()} cm';
|
||
}
|
||
|
||
/// Get formatted weight string for display
|
||
String get formattedWeight {
|
||
return '${weightKg.toStringAsFixed(1)} kg';
|
||
}
|
||
}
|