chunk method
The chunk method breaks the list into multiple, smaller list of a given size
Example:
list.chunk(2) // [1,2,3,4,5] -> [[1,2], [3,4], [5]]
Implementation
List<List<T>> chunk(int size) {
if (size < 1 && length < 1) {
return <List<T>>[];
}
return List<List<T>>.generate(
length ~/ size + (length % size == 0 ? 0 : 1),
(int index) => sublist(
index * size,
(index + 1) * size > length ? length : (index + 1) * size,
),
);
}