I'm trying to create nested Lists but the output I got is not what I have expected.
void main() {
List<int> row = List();
List<List<int>> rows = List();
for(int i=0;i<5;i++)
{
row.add(i+1);
row.add(i+2);
row.add(i+3);
rows.add(row);
row.clear();
}
print(rows);
}
The output I got :
[[], [], [], [], []]
The output I was Expecting:
[[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7]]
What changes can I should make run this code properly?
Listinstance (row). Since you ultimately clear that instance, you end up with aListof emptyLists. You want to add separateListinstances instead (e.g.rows.add([...row])orrows.add(List<int>.of(row)).)