maybeWhen<TResult extends Object?> method

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

A variant of when that fallback to an orElse callback.

It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case _:
    return orElse();
}

Implementation

@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
  TResult Function(
          int level, int index, String id, String title, bool isExpanded)?
      root,
  TResult Function(
          int level, int index, String id, String title, bool isExpanded)?
      module,
  TResult Function(
          int level, int index, String id, String title, bool isExpanded)?
      folder,
  TResult Function(int level, int index, String id, String title,
          bool isExpanded, Component component)?
      component,
  TResult Function(int level, int index, String id, String title,
          bool isExpanded, Component component, DocumentEntry document)?
      documentation,
  TResult Function(int level, int index, String id, String title,
          bool isExpanded, Component component, Story story)?
      story,
  required TResult orElse(),
}) {
  final _that = this;
  switch (_that) {
    case RootNode() when root != null:
      return root(
          _that.level, _that.index, _that.id, _that.title, _that.isExpanded);
    case ModuleNode() when module != null:
      return module(
          _that.level, _that.index, _that.id, _that.title, _that.isExpanded);
    case FolderNode() when folder != null:
      return folder(
          _that.level, _that.index, _that.id, _that.title, _that.isExpanded);
    case ComponentNode() when component != null:
      return component(_that.level, _that.index, _that.id, _that.title,
          _that.isExpanded, _that.component);
    case DocumentationNode() when documentation != null:
      return documentation(_that.level, _that.index, _that.id, _that.title,
          _that.isExpanded, _that.component, _that.document);
    case StoryNode() when story != null:
      return story(_that.level, _that.index, _that.id, _that.title,
          _that.isExpanded, _that.component, _that.story);
    case _:
      return orElse();
  }
}