replaceRange method
Removes the objects in the range start
inclusive to end
exclusive
and inserts the contents of replacement
in its place.
List<int> list = [1, 2, 3, 4, 5];
list.replaceRange(1, 4, [6, 7]);
list.join(', '); // '1, 6, 7, 5'
The provided range, given by start
and end
, must be valid.
A range from start
to end
is valid if 0 <= start <= end <= len
, where
len
is this list's length
. The range starts at start
and has length
end - start
. An empty range (with end == start
) is valid.
If any of replacements
's elements already exist in the list, a
DuplicateValueError will be thrown regardless of whether the list
is strict or not, unless the resulting list does not contain any duplicate
values once all values have been set.
Implementation
@override
void replaceRange(int start, int end, Iterable<E> replacement) {
// Check if any of the values in [replacement] already exist in the list.
for (var value in replacement) {
if (_contains(value)) {
// If so, check whether the list will contain any duplicate values once
// every value has been set.
final result = List<E>.of(elements)
..replaceRange(start, end, replacement);
if (_containsDuplicateValues(result, nullable: nullable)) {
throw DuplicateValueError(
_getDuplicateValue(result, nullable: nullable));
} else {
break;
}
}
}
elements.replaceRange(start, end, replacement);
}