when<TResult extends Object?> method
TResult
when<TResult extends Object?>({})
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(String projectName, String boxName) getKeys,
required TResult Function(String projectName) getBoxList,
required TResult Function(String projectName, String key, String boxName)
getValue,
}) {
final _that = this;
switch (_that) {
case HiveGetKeys():
return getKeys(_that.projectName, _that.boxName);
case HiveGetBoxs():
return getBoxList(_that.projectName);
case HiveGetValue():
return getValue(_that.projectName, _that.key, _that.boxName);
}
}