tapWithIndex<S> static method
Tap with a 0-based index.
tapWithIndex is similar to tap but the callback receives the index of each value in the sequence.
When to use
Use tapWithIndex when you need the index of each value in your side effect.
- Position Tracking: Tracking the position of values.
- Progress Reporting: Reporting progress through a sequence.
- Index-Based Logging: Logging with index information.
- Sequence Debugging: Debugging sequence order.
- Batch Processing: Tracking batch position.
How it works
- Each incoming pulse is type-checked.
- If the type matches,
onValueis called with the payload and index. - The index starts at 0 and increments on each typed pulse.
- If
onValuethrows an error, the pulse is dropped. - If
onValuesucceeds, the pulse is passed through unchanged. - The index is incremented after the callback.
Parameters:
source: The Source Cell. The cell providing the input values.onValue: Indexed Side Effect Callback. Called with each typed payload and its index. If it throws, the pulse is dropped.onError: Error Handler. Optional callback for handling errors.
Type Parameters:
S: The type of the payload.
Returns:
A FlowHandle that can be used to observe the tapped values.
Example
final items = Cell.ingress<Item>();
final handle = Flow.tapWithIndex<Item>(
items.cell,
onValue: (item, index) {
print('Processing item ${index + 1}');
},
);
See Also:
Implementation
static FlowHandle tapWithIndex<S>(
Cell source, {
required void Function(S value, int index) onValue,
TapErrorHandler? onError,
}) {
return TapWithIndex<S>(onValue, onError: onError).toHandle(source: source);
}