when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>({
  1. required TResult getKeys(
    1. String projectName,
    2. String boxName
    ),
  2. required TResult getBoxList(
    1. String projectName
    ),
  3. required TResult getValue(
    1. String projectName,
    2. String key,
    3. String boxName
    ),
})

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);
  }
}