when<TResult extends Object?> method

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

A switch-like method, using callbacks.

As opposed to map, this offers destructuring. It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case Subclass2(:final field2):
    return ...;
}

Implementation

@optionalTypeArgs
TResult when<TResult extends Object?>(
  TResult Function(
    String date,
    int userId,
    String email,
    String username,
    int requests,
    int tokens,
    Sub2ApiDecimal cost,
    Sub2ApiDecimal actualCost,
  )
  $default,
) {
  final _that = this;
  switch (_that) {
    case _Sub2ApiAdminUserUsageTrendPoint():
      return $default(
        _that.date,
        _that.userId,
        _that.email,
        _that.username,
        _that.requests,
        _that.tokens,
        _that.cost,
        _that.actualCost,
      );
    case _:
      throw StateError('Unexpected subclass');
  }
}