Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a 2d array in flutter / dart

I'm brand new to flutter and dart. I've searched google and all I can find is how to make 1d lists in flutter. I need a chart of values.

Specifically I need a row 12 long and a column 31 long filled with ascending numbers

1,  32,  63,

2,  33,  64, 

3,  34,  65,  etc....

thanks!

like image 262
tbunreal Avatar asked Sep 09 '19 20:09

tbunreal


4 Answers

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

var x = new List.generate(m, (_) => new List(n));
like image 123
Richard Rex Avatar answered Nov 14 '22 02:11

Richard Rex


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);
like image 34
Yuchen Avatar answered Nov 14 '22 01:11

Yuchen


int row = 3;
int col = 4;

var twoDList = List.generate(row, (i) => List(col), growable: false);

//For fill;
twoDList[0][1] = "deneme";

print(twoDList);

// [[null, deneme, null, null], [null, null, null, null], [null, null, null, null]]

Your 2D list having 3 rows and 4 columns.

like image 17
Smh Avatar answered Nov 14 '22 00:11

Smh


You can use smart_arrays_numerics package... look at this.. https://pub.dev/packages/smart_arrays_numerics

else you should use nested list.

List<List<int>>

and create your list with generator

like image 4
Vahab Ghadiri Avatar answered Nov 14 '22 00:11

Vahab Ghadiri