Store data in a 1-dimensional List
and add()
it to a 2-dimensional List
.
After add()
, .clear()
the one-dimensional List.
However, since List
refers to an address, clear()
loses the address, so the 2D array becomes an empty List. Is there a way to keep the values in a 2D List even after clearing?
List<int> num = [1, 2, 3];
List<List<int> num2 = [];
num2.add(num);
num.clear();
3
Answers
You could use
List.from()
to performshallow copy
use trick with
toList()
methodYou should add list as anonymous list