On this page
replaceRange method
void replaceRange(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 provide 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.
This method does not work on fixed-length lists, even when replacement
has the same number of elements as the replaced range. In that case use setRange
instead.
Source
void replaceRange(int start, int end, Iterable<E> newContents) {
RangeError.checkValidRange(start, end, this.length);
if (newContents is! EfficientLengthIterable) {
newContents = newContents.toList();
}
int removeLength = end - start;
int insertLength = newContents.length;
if (removeLength >= insertLength) {
int delta = removeLength - insertLength;
int insertEnd = start + insertLength;
int newLength = this.length - delta;
this.setRange(start, insertEnd, newContents);
if (delta != 0) {
this.setRange(insertEnd, newLength, this, end);
this.length = newLength;
}
} else {
int delta = insertLength - removeLength;
int newLength = this.length + delta;
int insertEnd = start + insertLength; // aka. end + delta.
this.length = newLength;
this.setRange(insertEnd, newLength, this, end);
this.setRange(start, insertEnd, newContents);
}
}
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-collection/ListMixin/replaceRange.html