when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. @JsonKey.new(name: 'id') String? id,
    2. @JsonKey.new(name: 'title') String? title,
    3. @JsonKey.new(name: 'onTapRoute') String? route,
    4. @JsonKey.new(name: 'icon') NavigationIcon? icon,
    5. @JsonKey.new(name: 'favourite') bool? isFavourite,
    6. @JsonKey.new(name: 'nested') 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');

}
}