mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? text(
    1. TextComponent value
    )?,
  2. TResult? card(
    1. CardComponent value
    )?,
  3. TResult? carousel(
    1. CarouselComponent value
    )?,
  4. TResult? productList(
    1. ProductListComponent value
    )?,
  5. TResult? image(
    1. ImageComponent value
    )?,
  6. TResult? buttonGroup(
    1. ButtonGroupComponent 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( TextComponent value)?  text,TResult? Function( CardComponent value)?  card,TResult? Function( CarouselComponent value)?  carousel,TResult? Function( ProductListComponent value)?  productList,TResult? Function( ImageComponent value)?  image,TResult? Function( ButtonGroupComponent value)?  buttonGroup,}){
final _that = this;
switch (_that) {
case TextComponent() when text != null:
return text(_that);case CardComponent() when card != null:
return card(_that);case CarouselComponent() when carousel != null:
return carousel(_that);case ProductListComponent() when productList != null:
return productList(_that);case ImageComponent() when image != null:
return image(_that);case ButtonGroupComponent() when buttonGroup != null:
return buttonGroup(_that);case _:
  return null;

}
}