List<E>.generate constructor

List<E>.generate(
  1. int length,
  2. E generator(
    1. int index
    ), {
  3. 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.

final growableList =
    List<int>.generate(3, (int index) => index * index, growable: true);
print(growableList); // [0, 1, 4]

final fixedLengthList =
    List<int>.generate(3, (int index) => index * index, growable: false);
print(fixedLengthList); // [0, 1, 4]

The created list is fixed-length if growable is set to false.

The length must be non-negative.

Implementation

external factory List.generate(
  int length,
  E generator(int index), {
  bool growable = true,
});