0

I want to implement below piece of code(Java) in Dart/Flutter. Could you please tell me how to achieve this.?

Here rows = 8, colums = not defined.

int[][] myArray = new int[8][];

1 Answers1

1

Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. ... Each element in the List is identified by a unique number called the index.

To get exactly what you want, you could use double List.generate, i.e.:

const cols = 31;
const rows = 12;
final array = List.generate(rows,
   (i) => List.generate(cols + 1, (j) => i + j * cols + 1, 
growable: false),growable: false);

array.forEach((row) {
print(row);
});
// [1, 32, 63, ...
// [2, 33, 64, ...
// [3, 34, 65, ...
// ...

There is also List.filled, where you can fill the array with some initial value when created. The following init the 2d array with 0s.

final array = List.generate(rows + 1, (i) => List.filled(cols + 1,  0,growable: false), growable: false);
AL MAMUN
  • 286
  • 2
  • 8