when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. String id,
    2. String? targetUserId,
    3. String? targetOrgId,
    4. GrantType grantType,
    5. PlanTier? plan,
    6. int? discountPct,
    7. int? aiBoostMessages,
    8. int? trialExtensionDays,
    9. DateTime startsAt,
    10. DateTime? endsAt,
    11. String? reason,
    12. String? grantedBy,
    13. DateTime createdAt,
    14. DateTime? revokedAt,
    15. String? revokedBy,
    )
)

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 id,  String? targetUserId,  String? targetOrgId,  GrantType grantType,  PlanTier? plan,  int? discountPct,  int? aiBoostMessages,  int? trialExtensionDays,  DateTime startsAt,  DateTime? endsAt,  String? reason,  String? grantedBy,  DateTime createdAt,  DateTime? revokedAt,  String? revokedBy)  $default,) {final _that = this;
switch (_that) {
case _AdminGrant():
return $default(_that.id,_that.targetUserId,_that.targetOrgId,_that.grantType,_that.plan,_that.discountPct,_that.aiBoostMessages,_that.trialExtensionDays,_that.startsAt,_that.endsAt,_that.reason,_that.grantedBy,_that.createdAt,_that.revokedAt,_that.revokedBy);case _:
  throw StateError('Unexpected subclass');

}
}