whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. int groupId,
    2. String groupName,
    3. int requests,
    4. int totalTokens,
    5. Sub2ApiDecimal cost,
    6. Sub2ApiDecimal actualCost,
    7. Sub2ApiDecimal accountCost,
    )?
)

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 groupId,
    String groupName,
    int requests,
    int totalTokens,
    Sub2ApiDecimal cost,
    Sub2ApiDecimal actualCost,
    Sub2ApiDecimal accountCost,
  )?
  $default,
) {
  final _that = this;
  switch (_that) {
    case _Sub2ApiAdminDashboardGroupStats() when $default != null:
      return $default(
        _that.groupId,
        _that.groupName,
        _that.requests,
        _that.totalTokens,
        _that.cost,
        _that.actualCost,
        _that.accountCost,
      );
    case _:
      return null;
  }
}