whenOrNull<TResult extends Object?> method
TResult?
whenOrNull<TResult extends Object?>({})
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()? empty,
TResult? Function(@JsonKey(name: "type") String type,
@JsonKey(name: "jsonString", toJson: modelToString) SpView model)?
sp,
TResult? Function(@JsonKey(name: "type") String type)? ping,
}) {
final _that = this;
switch (_that) {
case SocketSendModelEmpty() when empty != null:
return empty();
case SpViewSendModel() when sp != null:
return sp(_that.type, _that.model);
case PingViewSendModel() when ping != null:
return ping(_that.type);
case _:
return null;
}
}