whenOrNull<TResult extends Object?> method

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

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

}
}