insertOrAppend method
Inserts an element into the iterable at the specified index.
Takes the specified element and inserts it into the iterable at the
position index
.
If index
is less than zero, a RangeError is thrown.
If iteration of the underlying iterable is exausted before the position index
is reached, value
is added to the end iterable as if calling append.
Implementation
Iterable<T> insertOrAppend(int index, T value) sync* {
if (index == 0) {
yield* prepend(value);
return;
}
if (index < 0) {
throw RangeError.index(
index,
this,
'index',
'The index must not be negative.',
);
}
final iterator = this.iterator;
var i = 0;
while (iterator.moveNext()) {
if (i == index) {
yield value;
}
yield iterator.current;
i++;
}
if (index >= i) {
yield value;
}
}