when<TResult extends Object?> method
- @optionalTypeArgs
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: 'id') String? id,
@JsonKey(name: 'url') String? iconUrl,
@JsonKey(name: 'title') String? iconTitle,
@JsonKey(name: 'description') String? iconDescription)
$default,
) {
final _that = this;
switch (_that) {
case _NavigationIcon():
return $default(
_that.id, _that.iconUrl, _that.iconTitle, _that.iconDescription);
case _:
throw StateError('Unexpected subclass');
}
}