description

# List<E> constructor Null safety

List<E>(

1. [int? length]

)

Creates a list of the given length.

This constructor will throw an exception if `E` is not a nullable type. In this case, another constructor such as List.filled must be used instead.

The created list is fixed-length if `length` is provided.

    List fixedLengthList = new List(3);
    fixedLengthList.length;     // 3
    fixedLengthList.length = 1; // Error

The list has length 0 and is growable if `length` is omitted.

    List growableList = [];
    growableList.length; // 0;
    growableList.length = 3;

To create a growable list with a given length, for a nullable element type, just assign the length right after creation:

    List growableList = []..length = 500;

For a non-nullable element type, an alternative is the following:

    List<int> growableList = List<int>.filled(500, 0, growable: true);

The `length` must not be negative or null, if it is provided.

If the element type is not nullable, `length` must not be greater than zero.

This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use `[]` for a growable list or `List.empty` for a fixed length list (or where growability is determined at run-time).

## Implementation

    external factory List([int? length]);