chunk method

List<List<T>> chunk(
  1. int size
)

Breaks the list into multiple smaller lists of a given size.

size is the size of each chunk.

Example:

final chunks = list.chunk(3);

Implementation

List<List<T>> chunk(int size) {
  if (size < 1 || isEmpty) {
    return <List<T>>[];
  }

  return List<List<T>>.generate(
    (length / size).ceil(),
    (int index) => sublist(
      index * size,
      (index + 1) * size > length ? length : (index + 1) * size,
    ),
  );
}