whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. int id,
    2. int userId,
    3. String name,
    4. String status,
    5. List<String> ipWhitelist,
    6. List<String> ipBlacklist,
    7. Sub2ApiDecimal quota,
    8. Sub2ApiDecimal quotaUsed,
    9. int currentConcurrency,
    10. Sub2ApiKeyUsageWindow fiveHourWindow,
    11. Sub2ApiKeyUsageWindow oneDayWindow,
    12. Sub2ApiKeyUsageWindow sevenDayWindow,
    13. DateTime createdAt,
    14. DateTime updatedAt,
    15. int? groupId,
    16. DateTime? expiresAt,
    17. DateTime? lastUsedAt,
    18. String? lastUsedIp,
    )?
)

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(
    int id,
    int userId,
    String name,
    String status,
    List<String> ipWhitelist,
    List<String> ipBlacklist,
    Sub2ApiDecimal quota,
    Sub2ApiDecimal quotaUsed,
    int currentConcurrency,
    Sub2ApiKeyUsageWindow fiveHourWindow,
    Sub2ApiKeyUsageWindow oneDayWindow,
    Sub2ApiKeyUsageWindow sevenDayWindow,
    DateTime createdAt,
    DateTime updatedAt,
    int? groupId,
    DateTime? expiresAt,
    DateTime? lastUsedAt,
    String? lastUsedIp,
  )?
  $default,
) {
  final _that = this;
  switch (_that) {
    case _Sub2ApiKeySummary() when $default != null:
      return $default(
        _that.id,
        _that.userId,
        _that.name,
        _that.status,
        _that.ipWhitelist,
        _that.ipBlacklist,
        _that.quota,
        _that.quotaUsed,
        _that.currentConcurrency,
        _that.fiveHourWindow,
        _that.oneDayWindow,
        _that.sevenDayWindow,
        _that.createdAt,
        _that.updatedAt,
        _that.groupId,
        _that.expiresAt,
        _that.lastUsedAt,
        _that.lastUsedIp,
      );
    case _:
      return null;
  }
}