MIF_E31222656/lib/screens/calendar/field_model.dart

120 lines
3.4 KiB
Dart

class Field {
final String id;
final String name;
final int plotCount;
final String userId;
// Fields untuk data regional
final String? region;
final String? location;
final double? areaSize;
final String areaUnit;
final String ownershipType;
final String? ownerName;
final Map<String, dynamic>? regionSpecificData;
final DateTime createdAt;
final DateTime updatedAt;
Field({
required this.id,
required this.name,
required this.plotCount,
required this.userId,
this.region,
this.location,
this.areaSize,
this.areaUnit = '',
this.ownershipType = 'Milik Sendiri',
this.ownerName,
this.regionSpecificData,
required this.createdAt,
required this.updatedAt,
});
factory Field.fromJson(Map<String, dynamic> json) {
return Field(
id: json['id'],
name: json['name'] ?? 'Lahan Tanpa Nama',
plotCount:
json['plot_count'] is int
? json['plot_count']
: int.tryParse(json['plot_count']?.toString() ?? '1') ?? 1,
userId: json['user_id'],
region: json['region'],
location: json['location'],
areaSize:
json['area_size'] is double
? json['area_size']
: double.tryParse(json['area_size']?.toString() ?? '0'),
areaUnit: json['area_unit'] ?? '',
ownershipType: json['ownership_type'] ?? 'Milik Sendiri',
ownerName: json['owner_name'],
regionSpecificData:
json['region_specific_data'] is Map
? Map<String, dynamic>.from(json['region_specific_data'])
: null,
createdAt:
json['created_at'] != null
? json['created_at'] is DateTime
? json['created_at']
: DateTime.parse(json['created_at'])
: DateTime.now(),
updatedAt:
json['updated_at'] != null
? json['updated_at'] is DateTime
? json['updated_at']
: DateTime.parse(json['updated_at'])
: DateTime.now(),
);
}
// Alias untuk kompatibilitas dengan kode yang ada
factory Field.fromMap(Map<String, dynamic> map) => Field.fromJson(map);
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'plot_count': plotCount,
'user_id': userId,
'region': region,
'location': location,
'area_size': areaSize,
'area_unit': areaUnit,
'ownership_type': ownershipType,
'owner_name': ownerName,
'region_specific_data': regionSpecificData,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
};
}
Field copyWith({
String? name,
String? region,
String? location,
double? areaSize,
String? areaUnit,
int? plotCount,
String? ownershipType,
String? ownerName,
Map<String, dynamic>? regionSpecificData,
}) {
return Field(
id: id,
name: name ?? this.name,
region: region ?? this.region,
location: location ?? this.location,
areaSize: areaSize ?? this.areaSize,
areaUnit: areaUnit ?? this.areaUnit,
plotCount: plotCount ?? this.plotCount,
ownershipType: ownershipType ?? this.ownershipType,
ownerName: ownerName ?? this.ownerName,
regionSpecificData: regionSpecificData ?? this.regionSpecificData,
userId: userId,
createdAt: createdAt,
updatedAt: DateTime.now(),
);
}
}