diff --git a/lib/model/service_project/job_allocation_model.dart b/lib/model/service_project/job_allocation_model.dart index bc29a63..14ffefd 100644 --- a/lib/model/service_project/job_allocation_model.dart +++ b/lib/model/service_project/job_allocation_model.dart @@ -15,16 +15,26 @@ class ServiceProjectAllocationResponse { required this.timestamp, }); - factory ServiceProjectAllocationResponse.fromJson(Map json) { + factory ServiceProjectAllocationResponse.fromJson(Map? json) { + if (json == null) { + return ServiceProjectAllocationResponse( + success: false, + message: "", + data: [], + statusCode: 0, + timestamp: DateTime.now(), + ); + } + return ServiceProjectAllocationResponse( - success: json['success'] as bool, - message: json['message'] as String, - data: (json['data'] as List) + success: json['success'] as bool? ?? false, + message: json['message'] as String? ?? "", + data: (json['data'] as List? ?? []) .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 json) { + factory ServiceProjectAllocation.fromJson(Map? 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( - id: json['id'] as String, + 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 json) { + factory Project.fromJson(Map? json) { + if (json == null) { + return Project( + 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.parse(json['assignedDate'] as String), - contactName: json['contactName'] as String, - contactPhone: json['contactPhone'] as String, - contactEmail: json['contactEmail'] as String, + 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 json) { + factory Employee.fromJson(Map? json) { + if (json == null) { + return Employee( + 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, + 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 json) { + factory TeamRole.fromJson(Map? json) { + if (json == null) { + return TeamRole( + id: "", + name: "", + description: "", + ); + } + return TeamRole( - id: json['id'] as String, - name: json['name'] as String, - description: json['description'] as String, + id: json['id'] as String? ?? "", + name: json['name'] as String? ?? "", + description: json['description'] as String? ?? "", ); }