when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>({
  1. required TResult profileViewBasic(
    1. ProfileViewBasic data
    ),
  2. required TResult profileView(
    1. ProfileView data
    ),
  3. required TResult profileViewDetailed(
    1. ProfileViewDetailed data
    ),
})

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?>({required TResult Function( ProfileViewBasic data)  profileViewBasic,required TResult Function( ProfileView data)  profileView,required TResult Function( ProfileViewDetailed data)  profileViewDetailed,}) {final _that = this;
switch (_that) {
case UModerationSubjectProfileProfileViewBasic():
return profileViewBasic(_that.data);case UModerationSubjectProfileProfileView():
return profileView(_that.data);case UModerationSubjectProfileProfileViewDetailed():
return profileViewDetailed(_that.data);case _:
  throw StateError('Unexpected subclass');

}
}