ChunkTransformer<T> class

A StreamTransformer that splits incoming lists into smaller fixed-size chunks.

This transformer takes a stream of lists and emits sublists of at most chunkSize elements. Useful for processing or transmitting large lists in manageable parts, for example, when sending data over a network or writing to files in chunks.

The last chunk may be smaller than chunkSize if there are not enough remaining elements.

Example

final dataStream = Stream.value(List.generate(10000, (i) => i));
final chunkedStream = dataStream.transform(const ChunkTransformer<int>(chunkSize: 1024));
await for (final chunk in chunkedStream) {
  print('Chunk with ${chunk.length} items');
}

See also:

  • StreamTransformer, which is the base class for all stream transformations.
  • Stream.chunked (extension in package:collection), which splits iterables into chunks.
Implemented types

Constructors

ChunkTransformer({int chunkSize = 8192})
Creates a ChunkTransformer that emits sublists of at most chunkSize elements.
const

Properties

chunkSize int
The maximum number of elements in each emitted chunk.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

bind(Stream<List<T>> stream) Stream<List<T>>
Transforms the provided stream.
override
cast<RS, RT>() StreamTransformer<RS, RT>
Provides a StreamTransformer<RS, RT> view of this stream transformer.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited