whenOrNull<TResult extends Object?> method
TResult?
whenOrNull<TResult extends Object?>({})
A variant of when
that fallback to returning null
It is equivalent to doing:
switch (sealedClass) {
case Subclass(:final field):
return ...;
case _:
return null;
}
Implementation
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(String projectName, String boxName)? getKeys,
TResult? Function(String projectName)? getBoxList,
TResult? Function(String projectName, String key, String boxName)? getValue,
}) {
final _that = this;
switch (_that) {
case HiveGetKeys() when getKeys != null:
return getKeys(_that.projectName, _that.boxName);
case HiveGetBoxs() when getBoxList != null:
return getBoxList(_that.projectName);
case HiveGetValue() when getValue != null:
return getValue(_that.projectName, _that.key, _that.boxName);
case _:
return null;
}
}