whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. UserProfile? profile,
    2. SupplierProfile? supplierProfile,
    3. Customer? customerProfile,
    4. CommunicationSettings? communicationSettings,
    5. AuthCredentialResponse? auth,
    6. Navigation? navigation,
    )?
)

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(
          @JsonKey(name: 'profile') UserProfile? profile,
          @JsonKey(name: 'supplierProfile') SupplierProfile? supplierProfile,
          @JsonKey(name: 'customerProfile') Customer? customerProfile,
          @JsonKey(name: 'communicationSettings')
          CommunicationSettings? communicationSettings,
          @JsonKey(name: 'auth') AuthCredentialResponse? auth,
          @JsonKey(name: 'navigationActions') Navigation? navigation)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _UserResponse() when $default != null:
      return $default(
          _that.profile,
          _that.supplierProfile,
          _that.customerProfile,
          _that.communicationSettings,
          _that.auth,
          _that.navigation);
    case _:
      return null;
  }
}