mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? android(
    1. AndroidPlatform value
    )?,
  2. TResult? ios(
    1. IosPlatform value
    )?,
  3. TResult? macos(
    1. MacosPlatform value
    )?,
  4. TResult? web(
    1. WebPlatform value
    )?,
  5. TResult? linux(
    1. LinuxPlatform value
    )?,
  6. TResult? windows(
    1. WindowsPlatform value
    )?,
  7. TResult? fuchsia(
    1. FuchsiaPlatform value
    )?,
})

A variant of map that fallback to returning null.

It is equivalent to doing:

switch (sealedClass) {
  case final Subclass value:
    return ...;
  case _:
    return null;
}

Implementation

@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  TResult? Function(AndroidPlatform value)? android,
  TResult? Function(IosPlatform value)? ios,
  TResult? Function(MacosPlatform value)? macos,
  TResult? Function(WebPlatform value)? web,
  TResult? Function(LinuxPlatform value)? linux,
  TResult? Function(WindowsPlatform value)? windows,
  TResult? Function(FuchsiaPlatform value)? fuchsia,
}) {
  final _that = this;
  switch (_that) {
    case AndroidPlatform() when android != null:
      return android(_that);
    case IosPlatform() when ios != null:
      return ios(_that);
    case MacosPlatform() when macos != null:
      return macos(_that);
    case WebPlatform() when web != null:
      return web(_that);
    case LinuxPlatform() when linux != null:
      return linux(_that);
    case WindowsPlatform() when windows != null:
      return windows(_that);
    case FuchsiaPlatform() when fuchsia != null:
      return fuchsia(_that);
    case _:
      return null;
  }
}