chunked method
Splits the iterable into chunks of size.
The last chunk may be smaller.
[1, 2, 3, 4, 5].chunked(2); // ([1,2], [3,4], [5])
Implementation
Iterable<List<T>> chunked(int size) sync* {
if (size <= 0) {
throw ArgumentError.value(size, 'size', 'must be greater than zero');
}
var chunk = <T>[];
for (final e in this) {
chunk.add(e);
if (chunk.length == size) {
yield chunk;
chunk = [];
}
}
if (chunk.isNotEmpty) yield chunk;
}