List.generate factory constructor

List.generate(int length, E generator(int index), { bool growable: true })

Generates a list of values.

Creates a list with length positions and fills it with values created by calling generator for each index in the range 0 .. length - 1 in increasing order.

new List<int>.generate(3, (int index) => index * index); // [0, 1, 4]

The created list is fixed-length unless growable is true.

Source

factory List.generate(int length, E generator(int index),
    {bool growable: true}) {
  List<E> result;
  if (growable) {
    result = <E>[]..length = length;
  } else {
    result = new List<E>(length);
  }
  for (int i = 0; i < length; i++) {
    result[i] = generator(i);
  }
  return result;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-core/List/List.generate.html