How to copy List in dart without old reference?

后端 未结 7 1497
一个人的身影
一个人的身影 2021-01-17 11:14

Code will explain all:

//modal for list
class MyModal
{
 int myField1;
 List adjacentNodes;
 MyModal(this.myField1)
 {
  adjacentNodes= new Lis         


        
相关标签:
7条回答
  • 2021-01-17 11:55

    I was having the same problem, List.from() and List.of() didn't work. I had to map the current list, thus creating a new instance.

    List<CompanyBranchesDTO> get companyBranchesCopy {
      return companyBranches.map((e) =>
              CompanyBranchesDTO(id: e.id, name: e.name, isChecked: e.isChecked))
          .toList();
    }
    

    That way I have a new instance to inject where it is needed.

    0 讨论(0)
提交回复
热议问题