bytesChunks method

List<List<int>> bytesChunks(
  1. int chunkSize
)

Splits the string's UTF-16 code units (bytes) into a list of chunks.

This is useful for processing byte data in smaller segments. The last chunk may be smaller than chunkSize.

Example:

'Hello'.bytesChunks(2) ;  // [[72, 101], [108, 108], [111]]

Implementation

List<List<int>> bytesChunks(int chunkSize) {
  final chunks = <List<int>>[];
  final bytes = this.bytes();
  final len = bytes.length;
  for (int i = 0; i < len; i += chunkSize) {
    final size = i + chunkSize;
    chunks.add(bytes.sublist(i, size > len ? len : size));
  }
  return chunks;
}