whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. Sub2ApiAdminQuotaPlatform platform,
    2. Sub2ApiDecimal dailyUsageUsd,
    3. Sub2ApiDecimal? dailyLimitUsd,
    4. DateTime? dailyWindowResetsAt,
    5. DateTime? dailyWindowStart,
    6. Sub2ApiDecimal weeklyUsageUsd,
    7. Sub2ApiDecimal? weeklyLimitUsd,
    8. DateTime? weeklyWindowResetsAt,
    9. DateTime? weeklyWindowStart,
    10. Sub2ApiDecimal monthlyUsageUsd,
    11. Sub2ApiDecimal? monthlyLimitUsd,
    12. DateTime? monthlyWindowResetsAt,
    13. DateTime? monthlyWindowStart,
    )?
)

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(
    Sub2ApiAdminQuotaPlatform platform,
    Sub2ApiDecimal dailyUsageUsd,
    Sub2ApiDecimal? dailyLimitUsd,
    DateTime? dailyWindowResetsAt,
    DateTime? dailyWindowStart,
    Sub2ApiDecimal weeklyUsageUsd,
    Sub2ApiDecimal? weeklyLimitUsd,
    DateTime? weeklyWindowResetsAt,
    DateTime? weeklyWindowStart,
    Sub2ApiDecimal monthlyUsageUsd,
    Sub2ApiDecimal? monthlyLimitUsd,
    DateTime? monthlyWindowResetsAt,
    DateTime? monthlyWindowStart,
  )?
  $default,
) {
  final _that = this;
  switch (_that) {
    case _Sub2ApiAdminUserPlatformQuota() when $default != null:
      return $default(
        _that.platform,
        _that.dailyUsageUsd,
        _that.dailyLimitUsd,
        _that.dailyWindowResetsAt,
        _that.dailyWindowStart,
        _that.weeklyUsageUsd,
        _that.weeklyLimitUsd,
        _that.weeklyWindowResetsAt,
        _that.weeklyWindowStart,
        _that.monthlyUsageUsd,
        _that.monthlyLimitUsd,
        _that.monthlyWindowResetsAt,
        _that.monthlyWindowStart,
      );
    case _:
      return null;
  }
}