fillRange method Null safety
override
Overwrites a range of elements with fillValue
.
Sets the positions greater than or equal to start
and less than end
,
to fillValue
.
The provided range, given by start
and end
, must be valid.
A range from start
to end
is valid if 0 ≤ start
≤ end
≤ length.
An empty range (with end == start
) is valid.
Example:
var list = List.filled(5, -1);
print(list); // [-1, -1, -1, -1, -1]
list.fillRange(1, 3, 4);
print(list); // [-1, 4, 4, -1, -1]
If the element type is not nullable, the fillValue
must be provided and
must be non-null
.
Implementation
void fillRange(int start, int end, [E? fill]) {
// Hoist the case to fail eagerly if the user provides an invalid `null`
// value (or omits it) when E is a non-nullable type.
E value = fill as E;
RangeError.checkValidRange(start, end, this.length);
for (int i = start; i < end; i++) {
this[i] = value;
}
}