19

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!

11 Answers 11

24
  int row = 3;
  int col = 4;

  var twoDList = List<List>.generate(row, (i) => List<dynamic>.generate(col, (index) => null, growable: false), 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.

Sign up to request clarification or add additional context in comments.

3 Comments

The class 'List' doesn't have an unnamed constructor.
Could you please share updated code?
Solutions by Yuchen and Jamesdlin are better answers to the OP. Dynamic keyword should be avoided as much as possible. Here List<dynamic> would be better replaced by List<String?>. List<List> type annotation is also unnecessary, Dart will easily infer the corect type.
5

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

Comments

5

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

var x = new List.generate(m, (_) => new List(n));

2 Comments

Not sure why this one was upvoted, it is simply not correct Dart code. Since Dart 2 the new keyword is optional but more importantly List(n) will generate a syntax error.
The above code will throw error. Please refer to this (stackoverflow.com/a/60432222/19857564) or other answers.
5

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);

1 Comment

This is actually really smart. Though might have more complexity, the List.filled() is something that comes really handy. Thanks for sharing!
3

To declare and initialize the array 2D in the dart, you can use:

  List<List<int>> a = 
  [
    [10,   2,  4,  6,  -2],
    [ 1, -16,  6, -2,  -5],
    [ 0,   3, 10, -5,   1],
    [ 0,  -4,  1, 18,   2],
    [ 3,   1,  2,  2, -14],
  ];

Comments

2

maybe we can simply create reusable dart code.

void main(List<String> arguments) {
  var list = createDimensionList(4, 5);
  list[0][0] = 6;
  print(list);
  list[1][0] = 4;
  print(list);
}

List<List<int>> createDimensionList(int rows, int columns,
    {bool growable = false}) {
  final columnLength = List<int>.filled(columns, 0, growable: growable);
  var dataList =
      List.generate(rows, (i) => columnLength.toList(), growable: growable);
  return dataList;
}

//Output
[[6, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[6, 0, 0, 0, 0], [4, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Comments

1

I would use a nested collection-for:

const maxRows = 31;
const maxColumns = 12;
var matrix = [
  for (var row = 0; row < maxRows; row += 1)
    [for (var column = 0; column < maxColumns; column += 1)
      1 + row + column * maxRows],
];

Comments

1

To initialize a 2d matrix filled with 0 :

List.generate(numberRows, (_) => List.filled(numberColumns, 0));

Comments

1
void main() {
  List<dynamic> myArray = nd([12, 31])!;

  print(myArray);
}

List? nd(List<int> dimensions) {
  if (dimensions.isEmpty) {
    return null;
  } else {
    return List.filled(dimensions[0], nd(dimensions.sublist(1)));
  }
}

Comments

0

Just use this statement i got this same problem, Got resolved

var list1 = List.generate(12,(i) => List.generate(31, (j) =>{Your equation to genearte })));

Comments

-2

@vahab

so would I do

 final int NROWS = 12, NCOLS = 31;
  List<Float64List> matrix = List(NROWS);   
  for (int i = 0; i < 373; i++) {
    Float64List row = Float64List(NCOLS);

    for (int k = 0; k < NCOLS; k++) {        //I dont know what this does
      row[k] = double.parse("$i.$k");
    }
    matrix[i] = row;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.