split method

List<List<T>> split(
  1. int parts
)

Splits the list into a specified number of approximately equal parts.

parts is the number of parts to split the list into.

Example:

final splitList = list.split(2);

Implementation

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

  final int size = (length / parts).ceil();

  return List<List<T>>.generate(
    parts,
    (int i) => sublist(
      size * i,
      (i + 1) * size <= length ? (i + 1) * size : null,
    ),
  );
}