setRange method

  1. @override
void setRange(
  1. int start,
  2. int end,
  3. Iterable<E> iterable, [
  4. int skipCount = 0,
])

Copies the objects of iterable, skipping skipCount objects first, into the range start, inclusive, to end, exclusive, of the list.

    List<int> list1 = [1, 2, 3, 4];
    List<int> list2 = [5, 6, 7, 8, 9];
    // Copies the 4th and 5th items in list2 as the 2nd and 3rd items
    // of list1.
    list1.setRange(1, 3, list2, 3);
    list1.join(', '); // '1, 8, 9, 4'

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.

The iterable must have enough objects to fill the range from start to end after skipping skipCount objects.

If iterable is this list, the operation copies the elements originally in the range from skipCount to skipCount + (end - start) to the range start to end, even if the two ranges overlap.

If iterable depends on this list in some other way, no guarantees are made.

If any of iterable'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 setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
  assert(start >= 0 && start <= end);
  assert(end >= start && end <= length);
  assert(iterable.length >= end - start);
  assert(skipCount >= 0);

  // Check if any of the values in [iterable] already exist in the list.
  for (var value in iterable) {
    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)
        ..setRange(start, end, iterable, skipCount);

      if (_containsDuplicateValues(result, nullable: nullable)) {
        throw DuplicateValueError(
            _getDuplicateValue(result, nullable: nullable));
      } else {
        break;
      }
    }
  }

  return elements.setRange(start, end, iterable, skipCount);
}