I want to copy the elements of a 2D list into another list. Let the source list be List<List<int>> source, and the list to which I'm going to copy the elements into be List<List<int>> copied. And when I did-
void main() {
List<List<int>> source = [
[1],
[2],
[3]
];
List<List<int>> copied = List.from(source);
copied[2].add(5);
print(source);
print(copied);
}
The output came as-
[[1], [2], [3, 5]]
[[1], [2], [3, 5]]
The output I expected was-
[[1], [2], [3]]
[[1], [2], [3, 5]]
The source list seems to have been udpated, when I added 5 into copied . However, when I add a new list into copied, as such-
void main() {
List<List<int>> source = [
[1],
[2],
[3]
];
List<List<int>> copied = List.from(source);
copied.add([4]);
print(source);
print(copied);
}
The output is as expected-
[[1], [2], [3]]
[[1], [2], [3], [4]]
How do I make it so that all the elements are copied, without actually updating the source list? Thanks, in advance!