length property Null safety

int length

The number of boolean values in this list.

The valid indices for a list are 0 through length - 1.

If the list is growable, setting the length will change the number of values. Setting the length to a smaller number will remove all values with indices greater than or equal to the new length. Setting the length to a larger number will increase the number of values, and all the new values will be false.


int get length => _length;
void length=(int newLength)

Setting the length changes the number of elements in the list.

The list must be growable. If newLength is greater than current length, new entries are initialized to null, so newLength must not be greater than the current length if the element type E is non-nullable.

final maybeNumbers = <int?>[1, null, 3];
maybeNumbers.length = 5;
print(maybeNumbers); // [1, null, 3, null, null]
maybeNumbers.length = 2;
print(maybeNumbers); // [1, null]

final numbers = <int>[1, 2, 3];
numbers.length = 1;
print(numbers); // [1]
numbers.length = 5; // Throws, cannot add `null`s.


set length(int newLength);