match<R> method

R? match<R>({
  1. R waiting()?,
  2. R active(
    1. T
    )?,
  3. R error(
    1. dynamic
    )?,
  4. R done(
    1. T?,
    2. dynamic
    )?,
})

Maps the current status and value or error into a value.

Returns null if a callback is not provided for the active status. If done is null, active and error are used instead.

Implementation

R? match<R>(
    {R Function()? waiting,
    R Function(T)? active,
    // ignore:avoid_annotating_with_dynamic
    R Function(dynamic)? error,
    // ignore:avoid_annotating_with_dynamic
    R Function(T?, dynamic)? done}) {
  final status = _controller.status;
  if (status == StreamStatus.waiting) {
    return waiting == null ? null : waiting();
  }

  final data = _controller.data;
  final hasValue = _controller.valueType == _ValueType.value;
  final overrideDone = status == StreamStatus.done && done == null;
  final isActive = status == StreamStatus.active;

  if (isActive || overrideDone) {
    if (hasValue) {
      return active == null ? null : active(data as T);
    } else {
      return error == null ? null : error(data);
    }
  }
  return hasValue ? done!(data as T, null) : done!(null, data);
}