feat: enhance JSON parsing in ServiceProjectAllocationResponse and related models to handle null values gracefully
This commit is contained in:
parent
c44d10d35a
commit
d0b40a9822
@ -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(
|
return ServiceProjectAllocationResponse(
|
||||||
success: json['success'] as bool,
|
success: false,
|
||||||
message: json['message'] as String,
|
message: "",
|
||||||
data: (json['data'] as List<dynamic>)
|
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))
|
.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(
|
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']),
|
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(
|
return Project(
|
||||||
id: json['id'] as String,
|
id: "",
|
||||||
name: json['name'] as String,
|
name: "",
|
||||||
shortName: json['shortName'] as String,
|
shortName: "",
|
||||||
assignedDate: DateTime.parse(json['assignedDate'] as String),
|
assignedDate: DateTime.now(),
|
||||||
contactName: json['contactName'] as String,
|
contactName: "",
|
||||||
contactPhone: json['contactPhone'] as String,
|
contactPhone: "",
|
||||||
contactEmail: json['contactEmail'] as String,
|
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,
|
required this.jobRoleName,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory Employee.fromJson(Map<String, dynamic> json) {
|
factory Employee.fromJson(Map<String, dynamic>? json) {
|
||||||
|
if (json == null) {
|
||||||
return Employee(
|
return Employee(
|
||||||
id: json['id'] as String,
|
id: "",
|
||||||
firstName: json['firstName'] as String,
|
firstName: "",
|
||||||
lastName: json['lastName'] as String,
|
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?,
|
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(
|
return TeamRole(
|
||||||
id: json['id'] as String,
|
id: "",
|
||||||
name: json['name'] as String,
|
name: "",
|
||||||
description: json['description'] as String,
|
description: "",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return TeamRole(
|
||||||
|
id: json['id'] as String? ?? "",
|
||||||
|
name: json['name'] as String? ?? "",
|
||||||
|
description: json['description'] as String? ?? "",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user