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

@ -1,51 +1,65 @@
class GlobalProjectModel {
final String id;
final String name;
final String projectAddress;
final String contactPerson;
final DateTime startDate;
final DateTime endDate;
final int teamSize;
final String projectStatusId;
final String? tenantId;
final String id;
final String name;
final String projectAddress;
final String contactPerson;
final DateTime? startDate;
final DateTime? endDate;
final int teamSize;
final String projectStatusId;
final String? tenantId;
GlobalProjectModel({
required this.id,
required this.name,
required this.projectAddress,
required this.contactPerson,
required this.startDate,
required this.endDate,
required this.teamSize,
required this.projectStatusId,
this.tenantId,
});
GlobalProjectModel({
required this.id,
required this.name,
required this.projectAddress,
required this.contactPerson,
this.startDate,
this.endDate,
required this.teamSize,
required this.projectStatusId,
this.tenantId,
});
factory GlobalProjectModel.fromJson(Map<String, dynamic> json) {
return GlobalProjectModel(
id: json['id'] ?? '',
name: json['name'] ?? '',
projectAddress: json['projectAddress'] ?? '',
contactPerson: json['contactPerson'] ?? '',
startDate: DateTime.parse(json['startDate']),
endDate: DateTime.parse(json['endDate']),
teamSize: json['teamSize'] ?? 0, // SAFER
projectStatusId: json['projectStatusId'] ?? '',
tenantId: json['tenantId'],
);
factory GlobalProjectModel.fromJson(Map<String, dynamic> json) {
return GlobalProjectModel(
id: json['id'] ?? '',
name: json['name'] ?? '',
projectAddress: json['projectAddress'] ?? '',
contactPerson: json['contactPerson'] ?? '',
startDate: _parseDate(json['startDate']),
endDate: _parseDate(json['endDate']),
teamSize: json['teamSize'] is int
? json['teamSize']
: int.tryParse(json['teamSize']?.toString() ?? '0') ?? 0,
projectStatusId: json['projectStatusId'] ?? '',
tenantId: json['tenantId'],
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'projectAddress': projectAddress,
'contactPerson': contactPerson,
'startDate': startDate?.toIso8601String(),
'endDate': endDate?.toIso8601String(),
'teamSize': teamSize,
'projectStatusId': projectStatusId,
'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;
}
}
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'projectAddress': projectAddress,
'contactPerson': contactPerson,
'startDate': startDate.toIso8601String(),
'endDate': endDate.toIso8601String(),
'teamSize': teamSize,
'projectStatusId': projectStatusId,
'tenantId': tenantId,
};
}
}

View File

@ -3,8 +3,8 @@ class ProjectModel {
final String name;
final String projectAddress;
final String contactPerson;
final DateTime startDate;
final DateTime endDate;
final DateTime? startDate;
final DateTime? endDate;
final int teamSize;
final double completedWork;
final double plannedWork;
@ -16,8 +16,8 @@ class ProjectModel {
required this.name,
required this.projectAddress,
required this.contactPerson,
required this.startDate,
required this.endDate,
this.startDate,
this.endDate,
required this.teamSize,
required this.completedWork,
required this.plannedWork,
@ -25,36 +25,30 @@ class ProjectModel {
this.tenantId,
});
// Factory method to create an instance of ProjectModel from a JSON object
factory ProjectModel.fromJson(Map<String, dynamic> json) {
return ProjectModel(
id: json['id'],
name: json['name'],
projectAddress: json['projectAddress'],
contactPerson: json['contactPerson'],
startDate: DateTime.parse(json['startDate']),
endDate: DateTime.parse(json['endDate']),
teamSize: json['teamSize'],
completedWork: json['completedWork'] != null
? (json['completedWork'] as num).toDouble()
: 0.0,
plannedWork: json['plannedWork'] != null
? (json['plannedWork'] as num).toDouble()
: 0.0,
projectStatusId: json['projectStatusId'],
tenantId: json['tenantId'],
id: json['id']?.toString() ?? '',
name: json['name']?.toString() ?? '',
projectAddress: json['projectAddress']?.toString() ?? '',
contactPerson: json['contactPerson']?.toString() ?? '',
startDate: _parseDate(json['startDate']),
endDate: _parseDate(json['endDate']),
teamSize: _parseInt(json['teamSize']),
completedWork: _parseDouble(json['completedWork']),
plannedWork: _parseDouble(json['plannedWork']),
projectStatusId: json['projectStatusId']?.toString() ?? '',
tenantId: json['tenantId']?.toString(),
);
}
// Method to convert the ProjectModel instance back to a JSON object
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'projectAddress': projectAddress,
'contactPerson': contactPerson,
'startDate': startDate.toIso8601String(),
'endDate': endDate.toIso8601String(),
'startDate': startDate?.toIso8601String(),
'endDate': endDate?.toIso8601String(),
'teamSize': teamSize,
'completedWork': completedWork,
'plannedWork': plannedWork,
@ -62,4 +56,30 @@ class ProjectModel {
'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;
}
}