when<TResult extends Object?> method
TResult
when<TResult extends Object?>({
- required TResult root(),
- required TResult module(),
- required TResult folder(),
- required TResult component(),
- required TResult documentation(),
- required TResult 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);
}
}