when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<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 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(
          @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():
      return $default(
          _that.profile,
          _that.supplierProfile,
          _that.customerProfile,
          _that.communicationSettings,
          _that.auth,
          _that.navigation);
    case _:
      throw StateError('Unexpected subclass');
  }
}