Create an empty list of list

Creates a list of the given length.

NOTICE: 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].

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

var fixedLengthList = List[3]; fixedLengthList.length; // 3 fixedLengthList.length = 1; // Error

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

var growableList = List[]; 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 growableList = List.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.

Implementation

@Deprecated["Use a list literal, [], or the List.filled constructor instead"] external factory List[[int? length]];

Video liên quan

Chủ Đề