mappedLastOrNull<R> method

Future<R?> mappedLastOrNull<R>(
  1. R? mapper(
    1. T
    )
)

Maps the elements of the stream to a new type R using mapper. Tracks the mapped values and returns the last non-null mapped value, or null if the stream is empty or if all mapped values were null.

Implementation

Future<R?> mappedLastOrNull<R>(R? Function(T) mapper) {
  final completer = Completer<R?>();
  R? result;

  listen(
    (event) {
      final mapped = mapper(event);
      if (mapped != null) {
        result = mapped;
      }
    },
    onError: completer.completeError,
    onDone: () {
      completer.complete(result);
    },
    cancelOnError: true,
  );

  return completer.future;
}