pack method

List<List<T>> pack(
  1. int packSize
)

Converts list of elements to nested lists of elements where each sub-list contains max packSize items

Implementation

List<List<T>> pack(int packSize) {
  final packsCount = (length / packSize).ceil();
  final List<List<T>> result = List.generate(packsCount, (_) => <T>[]);

  int currentPack = 0;
  for (int i = 0; i < length; i++) {
    result[currentPack].add(this[i]);
    final num = i + 1;
    if (num % packSize == 0) {
      currentPack++;
    }
  }

  return result;
}