Dart Iterable replaceRange()

Dart Iterable replaceRange()

·

1 min read

replaceRange():

  • Replaces a range of elements with the elements of replacements.

  • Removes the objects in the range from start to end, then inserts the elements of replacements at start.

  • The provided range, given by start and end, must be valid.

Example:

extension IterableExtension<E> on Iterable<E> {
  Iterable<E> replaceRange(int start, int end, Iterable<E> newElements) sync* {
    yield* this.take(start);
    yield* newElements;
    yield* this.skip(end);
  }
}

void main() {
  List<int> myList = [1, 2, 3, 4, 5];
  List<int> newElements = [6, 7, 8];

  myList.replaceRange(1, 3, newElements);
  print(myList); // Output: [1, 6, 7, 8, 4, 5]
}
[1, 6, 7, 8, 4, 5]

Exited.