for loop - Initialize list of lists in Dart -
i have grid in dart implemented follows:
class cell { int row; int col; cell(this.row, this.col); } class grid { list<list<cell>> rows = new list(grid_size); grid() { rows.fillrange(0, rows.length, new list(grid_size)); } }
and can't seem find way initialize each cell proper row
, col
values: tried 2 nested loops, this
for(int = 0; < grid_size; i++) { for(int j = 0; j < grid_size; j++) { rows[i][j] = new cell(i, j); } }
but due dart's closure error protection described here, grid ends being populated cells having grid_size - 1
in row
member.
so, what's idiomatic way in dart initialize nested list?
i guess want:
class grid { list<list<cell>> rows; // = new list(grid_size); grid() { rows = new list.generate(grid_size, (i) => new list.generate(grid_size, (j) => new cell(i, j))); } }
Comments
Post a Comment