mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? paragraph(
    1. RichBlockParagraph value
    )?,
  2. TResult? heading(
    1. RichBlockSectionHeading value
    )?,
  3. TResult? pre(
    1. RichBlockPreformatted value
    )?,
  4. TResult? footer(
    1. RichBlockFooter value
    )?,
  5. TResult? divider(
    1. RichBlockDivider value
    )?,
  6. TResult? mathematicalExpression(
    1. RichBlockMathematicalExpression value
    )?,
  7. TResult? anchor(
    1. RichBlockAnchor value
    )?,
  8. TResult? list(
    1. RichBlockList value
    )?,
  9. TResult? blockquote(
    1. RichBlockBlockQuotation value
    )?,
  10. TResult? pullquote(
    1. RichBlockPullQuotation value
    )?,
  11. TResult? collage(
    1. RichBlockCollage value
    )?,
  12. TResult? slideshow(
    1. RichBlockSlideshow value
    )?,
  13. TResult? table(
    1. RichBlockTable value
    )?,
  14. TResult? details(
    1. RichBlockDetails value
    )?,
  15. TResult? map(
    1. RichBlockMap value
    )?,
  16. TResult? animation(
    1. RichBlockAnimation value
    )?,
  17. TResult? audio(
    1. RichBlockAudio value
    )?,
  18. TResult? photo(
    1. RichBlockPhoto value
    )?,
  19. TResult? video(
    1. RichBlockVideo value
    )?,
  20. TResult? voiceNote(
    1. RichBlockVoiceNote value
    )?,
  21. TResult? thinking(
    1. RichBlockThinking 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(RichBlockParagraph value)? paragraph,
  TResult? Function(RichBlockSectionHeading value)? heading,
  TResult? Function(RichBlockPreformatted value)? pre,
  TResult? Function(RichBlockFooter value)? footer,
  TResult? Function(RichBlockDivider value)? divider,
  TResult? Function(RichBlockMathematicalExpression value)?
  mathematicalExpression,
  TResult? Function(RichBlockAnchor value)? anchor,
  TResult? Function(RichBlockList value)? list,
  TResult? Function(RichBlockBlockQuotation value)? blockquote,
  TResult? Function(RichBlockPullQuotation value)? pullquote,
  TResult? Function(RichBlockCollage value)? collage,
  TResult? Function(RichBlockSlideshow value)? slideshow,
  TResult? Function(RichBlockTable value)? table,
  TResult? Function(RichBlockDetails value)? details,
  TResult? Function(RichBlockMap value)? map,
  TResult? Function(RichBlockAnimation value)? animation,
  TResult? Function(RichBlockAudio value)? audio,
  TResult? Function(RichBlockPhoto value)? photo,
  TResult? Function(RichBlockVideo value)? video,
  TResult? Function(RichBlockVoiceNote value)? voiceNote,
  TResult? Function(RichBlockThinking value)? thinking,
}) {
  final _that = this;
  switch (_that) {
    case RichBlockParagraph() when paragraph != null:
      return paragraph(_that);
    case RichBlockSectionHeading() when heading != null:
      return heading(_that);
    case RichBlockPreformatted() when pre != null:
      return pre(_that);
    case RichBlockFooter() when footer != null:
      return footer(_that);
    case RichBlockDivider() when divider != null:
      return divider(_that);
    case RichBlockMathematicalExpression()
        when mathematicalExpression != null:
      return mathematicalExpression(_that);
    case RichBlockAnchor() when anchor != null:
      return anchor(_that);
    case RichBlockList() when list != null:
      return list(_that);
    case RichBlockBlockQuotation() when blockquote != null:
      return blockquote(_that);
    case RichBlockPullQuotation() when pullquote != null:
      return pullquote(_that);
    case RichBlockCollage() when collage != null:
      return collage(_that);
    case RichBlockSlideshow() when slideshow != null:
      return slideshow(_that);
    case RichBlockTable() when table != null:
      return table(_that);
    case RichBlockDetails() when details != null:
      return details(_that);
    case RichBlockMap() when map != null:
      return map(_that);
    case RichBlockAnimation() when animation != null:
      return animation(_that);
    case RichBlockAudio() when audio != null:
      return audio(_that);
    case RichBlockPhoto() when photo != null:
      return photo(_that);
    case RichBlockVideo() when video != null:
      return video(_that);
    case RichBlockVoiceNote() when voiceNote != null:
      return voiceNote(_that);
    case RichBlockThinking() when thinking != null:
      return thinking(_that);
    case _:
      return null;
  }
}