when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. List<NavigationItem>? primaryActions,
    2. List<NavigationItem>? secondaryActions,
    3. int drawerSelectedIndex,
    4. int bottomBarSelectedIndex,
    )
)

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: 'primary') List<NavigationItem>? primaryActions,
          @JsonKey(name: 'secondary') List<NavigationItem>? secondaryActions,
          @JsonKey(defaultValue: -1) int drawerSelectedIndex,
          @JsonKey(defaultValue: 0) int bottomBarSelectedIndex)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _Navigation():
      return $default(_that.primaryActions, _that.secondaryActions,
          _that.drawerSelectedIndex, _that.bottomBarSelectedIndex);
    case _:
      throw StateError('Unexpected subclass');
  }
}