whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. List<StrategyWeight>? costs,
    2. List<XrayDuration>? baselines,
    3. int? expected,
    4. XrayDuration? maxRTT,
    5. double? tolerance,
    )?
)

A variant of when that fallback to returning null

It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case _:
    return null;
}

Implementation

@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  TResult? Function(
          List<StrategyWeight>? costs,
          List<XrayDuration>? baselines,
          int? expected,
          XrayDuration? maxRTT,
          double? tolerance)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _StrategyLeastLoadConfig() when $default != null:
      return $default(_that.costs, _that.baselines, _that.expected,
          _that.maxRTT, _that.tolerance);
    case _:
      return null;
  }
}