chunkedWithPadding method

Iterable<Iterable<E>> chunkedWithPadding(
  1. int size,
  2. E padding
)

Divides this Iterable into sub-lists of a given size. The final list is expanded with the provided padding, or null.

The following expression yields [1, 2], [3, 4], and [5, -1]:

[1, 2, 3, 4, 5].chunkedWithPadding(2, -1);

Implementation

Iterable<Iterable<E>> chunkedWithPadding(int size, E padding) sync* {
  final iterator = this.iterator;
  while (iterator.moveNext()) {
    final current = <E>[];
    do {
      current.add(iterator.current);
    } while (current.length < size && iterator.moveNext());
    while (current.length < size) {
      current.add(padding);
    }
    yield current.toList(growable: false);
  }
}