when<TResult extends Object?> method
TResult
when<TResult extends Object?>(
- TResult $default(
- String? id,
- String? title,
- String? route,
- NavigationIcon? icon,
- bool? isFavourite,
- List<
NavigationNestedItem> ? nestedItems,
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: 'title') String? title, @JsonKey(name: 'onTapRoute') String? route, @JsonKey(name: 'icon') NavigationIcon? icon, @JsonKey(name: 'favourite') bool? isFavourite, @JsonKey(name: 'nested') List<NavigationNestedItem>? nestedItems) $default,) {final _that = this;
switch (_that) {
case _NavigationItem():
return $default(_that.id,_that.title,_that.route,_that.icon,_that.isFavourite,_that.nestedItems);case _:
throw StateError('Unexpected subclass');
}
}