whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default()?, {
  2. TResult? spKeys()?,
  3. TResult? spGetValue(
    1. String data
    )?,
  4. TResult? setNewStringValue(
    1. String key,
    2. String newValue
    )?,
  5. TResult? setNewBoolValue(
    1. String key,
    2. bool newValue
    )?,
  6. TResult? setNewIntValue(
    1. String key,
    2. int newValue
    )?,
  7. TResult? setNewDoubleValue(
    1. String key,
    2. double newValue
    )?,
  8. TResult? removeSPKey(
    1. String key
    )?,
  9. TResult? removeAllKey()?,
})

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()? $default, {
  TResult? Function()? spKeys,
  TResult? Function(String data)? spGetValue,
  TResult? Function(String key, String newValue)? setNewStringValue,
  TResult? Function(String key, bool newValue)? setNewBoolValue,
  TResult? Function(String key, int newValue)? setNewIntValue,
  TResult? Function(String key, double newValue)? setNewDoubleValue,
  TResult? Function(String key)? removeSPKey,
  TResult? Function()? removeAllKey,
}) {
  final _that = this;
  switch (_that) {
    case SwiftActionDefault() when $default != null:
      return $default();
    case SwiftSpKeysAction() when spKeys != null:
      return spKeys();
    case SwiftSpGetValueAction() when spGetValue != null:
      return spGetValue(_that.data);
    case SwiftSetNewStringValueAction() when setNewStringValue != null:
      return setNewStringValue(_that.key, _that.newValue);
    case SwiftSetNewBoolValueAction() when setNewBoolValue != null:
      return setNewBoolValue(_that.key, _that.newValue);
    case SwiftSetNewIntValueAction() when setNewIntValue != null:
      return setNewIntValue(_that.key, _that.newValue);
    case SwiftSetNewDoubleValueAction() when setNewDoubleValue != null:
      return setNewDoubleValue(_that.key, _that.newValue);
    case SwiftRemoveSpKeyAction() when removeSPKey != null:
      return removeSPKey(_that.key);
    case SwiftRemoveSpAllKeysAction() when removeAllKey != null:
      return removeAllKey();
    case _:
      return null;
  }
}