when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>({
  1. required TResult root(
    1. int level,
    2. int index,
    3. String id,
    4. String title,
    5. bool isExpanded,
    ),
  2. required TResult module(
    1. int level,
    2. int index,
    3. String id,
    4. String title,
    5. bool isExpanded,
    ),
  3. required TResult folder(
    1. int level,
    2. int index,
    3. String id,
    4. String title,
    5. bool isExpanded,
    ),
  4. required TResult component(
    1. int level,
    2. int index,
    3. String id,
    4. String title,
    5. bool isExpanded,
    6. Component component,
    ),
  5. required TResult documentation(
    1. int level,
    2. int index,
    3. String id,
    4. String title,
    5. bool isExpanded,
    6. Component component,
    7. DocumentEntry document,
    ),
  6. required TResult story(
    1. int level,
    2. int index,
    3. String id,
    4. String title,
    5. bool isExpanded,
    6. Component component,
    7. Story story,
    ),
})

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(
          int level, int index, String id, String title, bool isExpanded)
      root,
  required TResult Function(
          int level, int index, String id, String title, bool isExpanded)
      module,
  required TResult Function(
          int level, int index, String id, String title, bool isExpanded)
      folder,
  required TResult Function(int level, int index, String id, String title,
          bool isExpanded, Component component)
      component,
  required TResult Function(int level, int index, String id, String title,
          bool isExpanded, Component component, DocumentEntry document)
      documentation,
  required TResult Function(int level, int index, String id, String title,
          bool isExpanded, Component component, Story story)
      story,
}) {
  final _that = this;
  switch (_that) {
    case RootNode():
      return root(
          _that.level, _that.index, _that.id, _that.title, _that.isExpanded);
    case ModuleNode():
      return module(
          _that.level, _that.index, _that.id, _that.title, _that.isExpanded);
    case FolderNode():
      return folder(
          _that.level, _that.index, _that.id, _that.title, _that.isExpanded);
    case ComponentNode():
      return component(_that.level, _that.index, _that.id, _that.title,
          _that.isExpanded, _that.component);
    case DocumentationNode():
      return documentation(_that.level, _that.index, _that.id, _that.title,
          _that.isExpanded, _that.component, _that.document);
    case StoryNode():
      return story(_that.level, _that.index, _that.id, _that.title,
          _that.isExpanded, _that.component, _that.story);
  }
}