mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? fill(
    1. BackgroundTypeFill value
    )?,
  2. TResult? wallpaper(
    1. BackgroundTypeWallpaper value
    )?,
  3. TResult? pattern(
    1. BackgroundTypePattern value
    )?,
  4. TResult? chatTheme(
    1. BackgroundTypeChatTheme 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(BackgroundTypeFill value)? fill,
  TResult? Function(BackgroundTypeWallpaper value)? wallpaper,
  TResult? Function(BackgroundTypePattern value)? pattern,
  TResult? Function(BackgroundTypeChatTheme value)? chatTheme,
}) {
  final _that = this;
  switch (_that) {
    case BackgroundTypeFill() when fill != null:
      return fill(_that);
    case BackgroundTypeWallpaper() when wallpaper != null:
      return wallpaper(_that);
    case BackgroundTypePattern() when pattern != null:
      return pattern(_that);
    case BackgroundTypeChatTheme() when chatTheme != null:
      return chatTheme(_that);
    case _:
      return null;
  }
}