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