feat: enhance JSON parsing in ServiceProjectAllocationResponse and related models to handle null values gracefully

This commit is contained in:
Vaibhav Surve 2025-11-20 11:55:15 +05:30
parent c44d10d35a
commit d0b40a9822

View File

@ -15,16 +15,26 @@ class ServiceProjectAllocationResponse {
required this.timestamp,
});
factory ServiceProjectAllocationResponse.fromJson(Map<String, dynamic> json) {
factory ServiceProjectAllocationResponse.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return ServiceProjectAllocationResponse(
success: json['success'] as bool,
message: json['message'] as String,
data: (json['data'] as List<dynamic>)
success: false,
message: "",
data: [],
statusCode: 0,
timestamp: DateTime.now(),
);
}
return ServiceProjectAllocationResponse(
success: json['success'] as bool? ?? false,
message: json['message'] as String? ?? "",
data: (json['data'] as List<dynamic>? ?? [])
.map((e) => ServiceProjectAllocation.fromJson(e))
.toList(),
errors: json['errors'],
statusCode: json['statusCode'] as int,
timestamp: DateTime.parse(json['timestamp'] as String),
statusCode: json['statusCode'] as int? ?? 0,
timestamp: DateTime.tryParse(json['timestamp'] ?? "") ?? DateTime.now(),
);
}
@ -61,17 +71,30 @@ class ServiceProjectAllocation {
this.reAssignedBy,
});
factory ServiceProjectAllocation.fromJson(Map<String, dynamic> json) {
factory ServiceProjectAllocation.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return ServiceProjectAllocation(
id: json['id'] as String,
id: "",
project: Project.fromJson(null),
employee: Employee.fromJson(null),
teamRole: TeamRole.fromJson(null),
isActive: false,
assignedAt: DateTime.now(),
assignedBy: Employee.fromJson(null),
);
}
return ServiceProjectAllocation(
id: json['id'] as String? ?? "",
project: Project.fromJson(json['project']),
employee: Employee.fromJson(json['employee']),
teamRole: TeamRole.fromJson(json['teamRole']),
isActive: json['isActive'] as bool,
assignedAt: DateTime.parse(json['assignedAt'] as String),
isActive: json['isActive'] as bool? ?? false,
assignedAt:
DateTime.tryParse(json['assignedAt'] ?? "") ?? DateTime.now(),
assignedBy: Employee.fromJson(json['assignedBy']),
reAssignedAt: json['reAssignedAt'] != null
? DateTime.parse(json['reAssignedAt'])
? DateTime.tryParse(json['reAssignedAt']) ?? DateTime.now()
: null,
reAssignedBy: json['reAssignedBy'] != null
? Employee.fromJson(json['reAssignedBy'])
@ -111,15 +134,28 @@ class Project {
required this.contactEmail,
});
factory Project.fromJson(Map<String, dynamic> json) {
factory Project.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return Project(
id: json['id'] as String,
name: json['name'] as String,
shortName: json['shortName'] as String,
assignedDate: DateTime.parse(json['assignedDate'] as String),
contactName: json['contactName'] as String,
contactPhone: json['contactPhone'] as String,
contactEmail: json['contactEmail'] as String,
id: "",
name: "",
shortName: "",
assignedDate: DateTime.now(),
contactName: "",
contactPhone: "",
contactEmail: "",
);
}
return Project(
id: json['id'] as String? ?? "",
name: json['name'] as String? ?? "",
shortName: json['shortName'] as String? ?? "",
assignedDate:
DateTime.tryParse(json['assignedDate'] ?? "") ?? DateTime.now(),
contactName: json['contactName'] as String? ?? "",
contactPhone: json['contactPhone'] as String? ?? "",
contactEmail: json['contactEmail'] as String? ?? "",
);
}
@ -153,15 +189,27 @@ class Employee {
required this.jobRoleName,
});
factory Employee.fromJson(Map<String, dynamic> json) {
factory Employee.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return Employee(
id: json['id'] as String,
firstName: json['firstName'] as String,
lastName: json['lastName'] as String,
id: "",
firstName: "",
lastName: "",
email: null,
photo: null,
jobRoleId: "",
jobRoleName: "",
);
}
return Employee(
id: json['id'] as String? ?? "",
firstName: json['firstName'] as String? ?? "",
lastName: json['lastName'] as String? ?? "",
email: json['email'] as String?,
photo: json['photo'] as String?,
jobRoleId: json['jobRoleId'] as String,
jobRoleName: json['jobRoleName'] as String,
jobRoleId: json['jobRoleId'] as String? ?? "",
jobRoleName: json['jobRoleName'] as String? ?? "",
);
}
@ -187,11 +235,19 @@ class TeamRole {
required this.description,
});
factory TeamRole.fromJson(Map<String, dynamic> json) {
factory TeamRole.fromJson(Map<String, dynamic>? json) {
if (json == null) {
return TeamRole(
id: json['id'] as String,
name: json['name'] as String,
description: json['description'] as String,
id: "",
name: "",
description: "",
);
}
return TeamRole(
id: json['id'] as String? ?? "",
name: json['name'] as String? ?? "",
description: json['description'] as String? ?? "",
);
}