mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? photo(
    1. InputMediaPhoto value
    )?,
  2. TResult? document(
    1. InputMediaDocument value
    )?,
  3. TResult? animation(
    1. InputMediaAnimation value
    )?,
  4. TResult? audio(
    1. InputMediaAudio value
    )?,
  5. TResult? video(
    1. InputMediaVideo 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(InputMediaPhoto value)? photo,
  TResult? Function(InputMediaDocument value)? document,
  TResult? Function(InputMediaAnimation value)? animation,
  TResult? Function(InputMediaAudio value)? audio,
  TResult? Function(InputMediaVideo value)? video,
}) {
  final _that = this;
  switch (_that) {
    case InputMediaPhoto() when photo != null:
      return photo(_that);
    case InputMediaDocument() when document != null:
      return document(_that);
    case InputMediaAnimation() when animation != null:
      return animation(_that);
    case InputMediaAudio() when audio != null:
      return audio(_that);
    case InputMediaVideo() when video != null:
      return video(_that);
    case _:
      return null;
  }
}