WithPrevious<T> constructor

WithPrevious<T>([
  1. T? initialPrevious
])

Creates a WithPrevious transformer.

A StreamTransformer that pairs each value in a stream with its previous value.

This transformer takes each value from the source stream and emits a record containing both the current value and the previous value. The first emission will have the initialPrevious value, if provided, as the previous value.

The transformed stream emits a PreviousValuePair, which is a record type containing:

  • previous: The previous value in the stream (or initialPrevious for the first emission)
  • current: The current value from the stream

Example:

final stream = Stream.fromIterable([1, 2, 3]);
final withPrevious = stream.transform(WithPrevious(0));
// Emits: (previous: 0, current: 1)
//        (previous: 1, current: 2)
//        (previous: 2, current: 3)

Implementation

WithPrevious([this.initialPrevious]);