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