whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. int userId,
    2. String email,
    3. String username,
    4. Sub2ApiDecimal actualCost,
    5. int requests,
    6. int tokens,
    )?
)

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(
    int userId,
    String email,
    String username,
    Sub2ApiDecimal actualCost,
    int requests,
    int tokens,
  )?
  $default,
) {
  final _that = this;
  switch (_that) {
    case _Sub2ApiAdminUserSpendingRankingItem() when $default != null:
      return $default(
        _that.userId,
        _that.email,
        _that.username,
        _that.actualCost,
        _that.requests,
        _that.tokens,
      );
    case _:
      return null;
  }
}