我有一个不可变对象(公司),其中包含一个不可变对象列表(部门),其中包含一个不可变对象列表(员工)。
@freezed
class Company with _$Company {
factory Company({String? name, List<Department>? departments}) = _Company;
}
@freezed
class Department with _$Department {
factory Department({String? name, List<Employee>? employees}) = _Department;
}
@freezed
class Employee with _$Employee {
factory Employee({String? name, int? age}) = _Employee;
}
我需要更改特定员工的姓名(实现以下功能)
Company updateEmployeeName(Company company, int DepartmentIndex,int EmployeeIndex){
...
}
这是我的解决方案,但我想知道是否有更简单的方法:
Company updateEmployeeName(Company company, int DepartmentIndex,int EmployeeIndex){
return company.copyWith(
departments: company.departments!
..[departmentIndex] = company.departments![departmentIndex].copyWith(
employees: company.departments![departmentIndex].employees!
..[employeeIndex] = company
.departments![departmentIndex].employees![employeeIndex]
.copyWith(name: 'Ali')));
}