whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  1. TResult? invalidPath()?,
  2. TResult? noTags()?,
  3. TResult? openFile(
    1. String message
    )?,
  4. TResult? read(
    1. String message
    )?,
  5. TResult? write(
    1. String message
    )?,
  6. TResult? remote(
    1. String message
    )?,
})

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()? invalidPath,
  TResult? Function()? noTags,
  TResult? Function(String message)? openFile,
  TResult? Function(String message)? read,
  TResult? Function(String message)? write,
  TResult? Function(String message)? remote,
}) {
  final _that = this;
  switch (_that) {
    case HaudiotaggerError_InvalidPath() when invalidPath != null:
      return invalidPath();
    case HaudiotaggerError_NoTags() when noTags != null:
      return noTags();
    case HaudiotaggerError_OpenFile() when openFile != null:
      return openFile(_that.message);
    case HaudiotaggerError_Read() when read != null:
      return read(_that.message);
    case HaudiotaggerError_Write() when write != null:
      return write(_that.message);
    case HaudiotaggerError_Remote() when remote != null:
      return remote(_that.message);
    case _:
      return null;
  }
}