feat: Update GlobalProjectModel and ProjectModel to handle nullable date fields and improve JSON parsing

This commit is contained in:
Vaibhav Surve 2025-09-30 11:03:33 +05:30
parent 7f924ee533
commit 38ae9e3571
2 changed files with 104 additions and 70 deletions

View File

@ -3,8 +3,8 @@ final String id;
final String name; final String name;
final String projectAddress; final String projectAddress;
final String contactPerson; final String contactPerson;
final DateTime startDate; final DateTime? startDate;
final DateTime endDate; final DateTime? endDate;
final int teamSize; final int teamSize;
final String projectStatusId; final String projectStatusId;
final String? tenantId; final String? tenantId;
@ -14,8 +14,8 @@ required this.id,
required this.name, required this.name,
required this.projectAddress, required this.projectAddress,
required this.contactPerson, required this.contactPerson,
required this.startDate, this.startDate,
required this.endDate, this.endDate,
required this.teamSize, required this.teamSize,
required this.projectStatusId, required this.projectStatusId,
this.tenantId, this.tenantId,
@ -27,9 +27,11 @@ id: json['id'] ?? '',
name: json['name'] ?? '', name: json['name'] ?? '',
projectAddress: json['projectAddress'] ?? '', projectAddress: json['projectAddress'] ?? '',
contactPerson: json['contactPerson'] ?? '', contactPerson: json['contactPerson'] ?? '',
startDate: DateTime.parse(json['startDate']), startDate: _parseDate(json['startDate']),
endDate: DateTime.parse(json['endDate']), endDate: _parseDate(json['endDate']),
teamSize: json['teamSize'] ?? 0, // SAFER teamSize: json['teamSize'] is int
? json['teamSize']
: int.tryParse(json['teamSize']?.toString() ?? '0') ?? 0,
projectStatusId: json['projectStatusId'] ?? '', projectStatusId: json['projectStatusId'] ?? '',
tenantId: json['tenantId'], tenantId: json['tenantId'],
); );
@ -41,11 +43,23 @@ return {
'name': name, 'name': name,
'projectAddress': projectAddress, 'projectAddress': projectAddress,
'contactPerson': contactPerson, 'contactPerson': contactPerson,
'startDate': startDate.toIso8601String(), 'startDate': startDate?.toIso8601String(),
'endDate': endDate.toIso8601String(), 'endDate': endDate?.toIso8601String(),
'teamSize': teamSize, 'teamSize': teamSize,
'projectStatusId': projectStatusId, 'projectStatusId': projectStatusId,
'tenantId': tenantId, 'tenantId': tenantId,
}; };
} }
static DateTime? _parseDate(dynamic value) {
if (value == null || value.toString().trim().isEmpty) {
return null;
}
try {
return DateTime.parse(value.toString());
} catch (e) {
print('⚠️ Failed to parse date "$value": $e');
return null;
}
}
} }

View File

@ -3,8 +3,8 @@ class ProjectModel {
final String name; final String name;
final String projectAddress; final String projectAddress;
final String contactPerson; final String contactPerson;
final DateTime startDate; final DateTime? startDate;
final DateTime endDate; final DateTime? endDate;
final int teamSize; final int teamSize;
final double completedWork; final double completedWork;
final double plannedWork; final double plannedWork;
@ -16,8 +16,8 @@ class ProjectModel {
required this.name, required this.name,
required this.projectAddress, required this.projectAddress,
required this.contactPerson, required this.contactPerson,
required this.startDate, this.startDate,
required this.endDate, this.endDate,
required this.teamSize, required this.teamSize,
required this.completedWork, required this.completedWork,
required this.plannedWork, required this.plannedWork,
@ -25,36 +25,30 @@ class ProjectModel {
this.tenantId, this.tenantId,
}); });
// Factory method to create an instance of ProjectModel from a JSON object
factory ProjectModel.fromJson(Map<String, dynamic> json) { factory ProjectModel.fromJson(Map<String, dynamic> json) {
return ProjectModel( return ProjectModel(
id: json['id'], id: json['id']?.toString() ?? '',
name: json['name'], name: json['name']?.toString() ?? '',
projectAddress: json['projectAddress'], projectAddress: json['projectAddress']?.toString() ?? '',
contactPerson: json['contactPerson'], contactPerson: json['contactPerson']?.toString() ?? '',
startDate: DateTime.parse(json['startDate']), startDate: _parseDate(json['startDate']),
endDate: DateTime.parse(json['endDate']), endDate: _parseDate(json['endDate']),
teamSize: json['teamSize'], teamSize: _parseInt(json['teamSize']),
completedWork: json['completedWork'] != null completedWork: _parseDouble(json['completedWork']),
? (json['completedWork'] as num).toDouble() plannedWork: _parseDouble(json['plannedWork']),
: 0.0, projectStatusId: json['projectStatusId']?.toString() ?? '',
plannedWork: json['plannedWork'] != null tenantId: json['tenantId']?.toString(),
? (json['plannedWork'] as num).toDouble()
: 0.0,
projectStatusId: json['projectStatusId'],
tenantId: json['tenantId'],
); );
} }
// Method to convert the ProjectModel instance back to a JSON object
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
return { return {
'id': id, 'id': id,
'name': name, 'name': name,
'projectAddress': projectAddress, 'projectAddress': projectAddress,
'contactPerson': contactPerson, 'contactPerson': contactPerson,
'startDate': startDate.toIso8601String(), 'startDate': startDate?.toIso8601String(),
'endDate': endDate.toIso8601String(), 'endDate': endDate?.toIso8601String(),
'teamSize': teamSize, 'teamSize': teamSize,
'completedWork': completedWork, 'completedWork': completedWork,
'plannedWork': plannedWork, 'plannedWork': plannedWork,
@ -62,4 +56,30 @@ class ProjectModel {
'tenantId': tenantId, 'tenantId': tenantId,
}; };
} }
// ---------- Helpers ----------
static DateTime? _parseDate(dynamic value) {
if (value == null || value.toString().trim().isEmpty) {
return null;
}
try {
return DateTime.parse(value.toString());
} catch (e) {
print('⚠️ Failed to parse date: $value');
return null;
}
}
static int _parseInt(dynamic value) {
if (value == null) return 0;
if (value is int) return value;
return int.tryParse(value.toString()) ?? 0;
}
static double _parseDouble(dynamic value) {
if (value == null) return 0.0;
if (value is num) return value.toDouble();
return double.tryParse(value.toString()) ?? 0.0;
}
} }