when<TResult extends Object?> method
- @optionalTypeArgs
- TResult $default(
- int userId,
- String email,
- int requests,
- int inputTokens,
- int outputTokens,
- int cacheTokens,
- int totalTokens,
- Sub2ApiDecimal cost,
- Sub2ApiDecimal actualCost,
- Sub2ApiDecimal accountCost,
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(
int userId,
String email,
int requests,
int inputTokens,
int outputTokens,
int cacheTokens,
int totalTokens,
Sub2ApiDecimal cost,
Sub2ApiDecimal actualCost,
Sub2ApiDecimal accountCost,
)
$default,
) {
final _that = this;
switch (_that) {
case _Sub2ApiAdminUserBreakdownItem():
return $default(
_that.userId,
_that.email,
_that.requests,
_that.inputTokens,
_that.outputTokens,
_that.cacheTokens,
_that.totalTokens,
_that.cost,
_that.actualCost,
_that.accountCost,
);
case _:
throw StateError('Unexpected subclass');
}
}