chunks method

List<String> chunks(
  1. int chunkSize
)

Splits the string into a list of chunks of a specified size.

The last chunk may be smaller than chunkSize if the string's length is not evenly divisible by chunkSize.

Example:

'HelloWorld'.chunks( 3) ;  // ['Hel', 'loW', 'orl', 'd']
'12345'.chunks(2); // ['12', '34', '5']

Implementation

List<String> chunks(int chunkSize) {
  final chunks = <String>[];
  if (this == null) {
    return chunks;
  }
  final len = this!.length;
  for (int i = 0; i < len; i += chunkSize) {
    final size = i + chunkSize;
    chunks.add(this!.substring(i, size > len ? len : size));
  }
  return chunks;
}