maybeMap<TResult extends Object?> method

  1. @optionalTypeArgs
TResult maybeMap<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
    )?,
  5. required TResult orElse(),
})

A variant of map that fallback to returning orElse.

It is equivalent to doing:

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

Implementation

@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
  TResult Function(BackgroundTypeFill value)? fill,
  TResult Function(BackgroundTypeWallpaper value)? wallpaper,
  TResult Function(BackgroundTypePattern value)? pattern,
  TResult Function(BackgroundTypeChatTheme value)? chatTheme,
  required TResult orElse(),
}) {
  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 orElse();
  }
}