slices method Null safety

Iterable<List<E>> slices(
  1. int length
)

Contiguous slices of this with the given length.

Each slice is a view of this list length elements long, except for the last one which may be shorter if this contains too few elements. Each slice begins after the last one ends.

As with slice, these slices are backed by this list, which must not change its length while the views are being used.

For example, [1, 2, 3, 4, 5].slices(2) returns [[1, 2], [3, 4], [5]].

Implementation

Iterable<List<E>> slices(int length) sync* {
  if (length < 1) throw RangeError.range(length, 1, null, 'length');
  for (var i = 0; i < this.length; i += length) {
    yield slice(i, min(i + length, this.length));
  }
}