maybeWhen<TResult extends Object?> method

  1. @optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
  1. TResult getKeys(
    1. String projectName,
    2. String boxName
    )?,
  2. TResult getBoxList(
    1. String projectName
    )?,
  3. TResult getValue(
    1. String projectName,
    2. String key,
    3. String boxName
    )?,
  4. 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(String projectName, String boxName)? getKeys,
  TResult Function(String projectName)? getBoxList,
  TResult Function(String projectName, String key, String boxName)? getValue,
  required TResult orElse(),
}) {
  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 orElse();
  }
}