深拷贝
https://stackoverflow.com/questions/21744480/clone-a-list-map-or-set-in-dart
For lists and sets, I typically use
List<String> clone = []..addAll(originalList);
The caveat, as @kzhdev mentions, is that addAll()
and from()
[Do] not really make a clone. They add a reference in the new Map/List/Set.
That's usually ok with me, but I would keep it in mind.
=======================================================================================================
This solution should work:
List list1 = [1,2,3,4];
List list2 = list1.map((element)=>element).toList();
It's for a list but should work the same for a map etc, remember to add to list if its a list at the end
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LIst 多层情况下的addall, map表现, (无效); 还是循环到最底层生效了...
import 'dart:io';
import 'dart:convert';
import 'dart:math';
main(){
List startList = [];
startList.addAll(spareParts[Random().nextInt(7)]);
print(startList);
startList[0][0] = 999;
print(';;;;;;;;;;;;;;;');
print('startList $startList');
print('spareParts $spareParts');
List ddd = startList.map((v)=>v).toList();
print('ddd: $ddd');
ddd[0][0] = 88888;
print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
print('ddd: $ddd');
print('startList $startList');
print('spareParts $spareParts');
print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
ddd = [[],[],[],[]];
for(int y=0; y<4; y++){
for(int x=0; x<4; x++){
ddd[y].add(startList[y][x]);
}
}
print('ddd$ddd');
print('startList $startList');
print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
ddd[0][0] = 555555555;
print('ddd$ddd');
print('startList $startList');
print('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm');
}
List spareParts = [
[[0,1,0,0],
[0,1,0,0],
[0,1,0,0],
[0,1,0,0]],
[[0,0,0,0],
[0,1,0,0],
[1,1,1,0],
[0,0,0,0]],
[[0,0,0,0],
[1,1,1,0],
[0,0,1,0],
[0,0,0,0]],
[[0,0,0,0],
[0,1,1,1],
[0,1,0,0],
[0,0,0,0]],
[[0,0,0,0],
[1,1,0,0],
[0,1,1,0],
[0,0,0,0]],
[[0,0,0,0],
[0,0,1,1],
[0,1,1,0],
[0,0,0,0]],
[[0,0,0,0],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0]],
];
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++