首页 > 解决方案 > 如何在飞镖中深度复制嵌套列表?

问题描述

如何在 dart 中创建嵌套列表的副本?在此代码中,我对副本所做的更改也在原始版本中进行了更改

List board = [[0,0,0], [0,0,0], [0,0,0]];
List boardCopy = List.from(board); // create copy of the board

boardCopy[0][0] = 1; // change copy

print(board); // print original board

OUTPUT:
  [[1,0,0], [0,0,0], [0,0,0]] <-- it has changed the original board!!!

标签: listflutterdartnested-listsdeep-copy

解决方案


我解决了:

List boardCopy = board.map((element) => List.from(element)).toList();

推荐阅读