when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. int id,
    2. int userId,
    3. int groupId,
    4. DateTime startsAt,
    5. DateTime expiresAt,
    6. String status,
    7. DateTime? dailyWindowStart,
    8. DateTime? weeklyWindowStart,
    9. DateTime? monthlyWindowStart,
    10. Sub2ApiDecimal dailyUsageUsd,
    11. Sub2ApiDecimal weeklyUsageUsd,
    12. Sub2ApiDecimal monthlyUsageUsd,
    13. DateTime createdAt,
    14. DateTime updatedAt,
    15. DateTime? revokedAt,
    16. Sub2ApiCommerceGroupSummary? group,
    )
)

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 id,
    int userId,
    int groupId,
    DateTime startsAt,
    DateTime expiresAt,
    String status,
    DateTime? dailyWindowStart,
    DateTime? weeklyWindowStart,
    DateTime? monthlyWindowStart,
    Sub2ApiDecimal dailyUsageUsd,
    Sub2ApiDecimal weeklyUsageUsd,
    Sub2ApiDecimal monthlyUsageUsd,
    DateTime createdAt,
    DateTime updatedAt,
    DateTime? revokedAt,
    Sub2ApiCommerceGroupSummary? group,
  )
  $default,
) {
  final _that = this;
  switch (_that) {
    case _Sub2ApiUserSubscription():
      return $default(
        _that.id,
        _that.userId,
        _that.groupId,
        _that.startsAt,
        _that.expiresAt,
        _that.status,
        _that.dailyWindowStart,
        _that.weeklyWindowStart,
        _that.monthlyWindowStart,
        _that.dailyUsageUsd,
        _that.weeklyUsageUsd,
        _that.monthlyUsageUsd,
        _that.createdAt,
        _that.updatedAt,
        _that.revokedAt,
        _that.group,
      );
    case _:
      throw StateError('Unexpected subclass');
  }
}