chunked<R> method
Splits into a list of lists, each not exceeding the given size
.
The last list may have less elements than the given size
.
The returned iterable will be lazy, it will only chunk the elements that are requested.
final words = 'one two three four five six seven eight nine ten'.split(' ');
final chunks = words.chunked(3);
print(chunks); // ([one, two, three], [four, five, six], [seven, eight, nine], [ten])
When the transform
is given, it will be applied to each list.
final numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
final chunkedAndReversed = numbers.chunked(3, (chunk) => chunk.reversed);
print (chunkedAndReversed); // ([3, 2, 1], [6, 5 ,4], [9, 8 ,7], [10])
Implementation
Iterable<R> chunked<R>(int size, [ChunkedTransform<List<T>, R>? transform]) {
transform ??= (v) => v as R;
return _Chunked<T, R>(this, size, transform);
}