when<TResult extends Object?> method
TResult
when<TResult extends Object?>({
- required TResult text(),
- required TResult card(),
- required TResult carousel(
- String id,
- List<
RichComponent> items, - CarouselConfig? config
- required TResult productList(
- String id,
- List<
RichComponent> items, - ProductListConfig? config
- required TResult image(
- String id,
- String url,
- String? alt,
- String? caption,
- @JsonKey.new(name: 'aspect_ratio') String? aspectRatio,
- List<
RichAction> ? actions, - @JsonKey.new(name: 'on_tap_callback') OnTapCallback? onTapCallback,
- required TResult buttonGroup(
- String id,
- List<
RichButton> buttons, - String layout
- required TResult swatchGrid(
- String id,
- List<
RichSwatch> swatches, - int columns
- required TResult hostSlot(),
A switch-like method, using callbacks.
As opposed to map, this offers destructuring.
It is equivalent to doing:
switch (sealedClass) {
case Subclass(:final field):
return ...;
case Subclass2(:final field2):
return ...;
}
Implementation
@optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function( String content, String format) text,required TResult Function( String id, String title, RichImage? image, String? subtitle, String? description, @JsonKey(fromJson: _metadataFromJson) Map<String, String>? metadata, List<RichAction>? actions) card,required TResult Function( String id, List<RichComponent> items, CarouselConfig? config) carousel,required TResult Function( String id, List<RichComponent> items, ProductListConfig? config) productList,required TResult Function( String id, String url, String? alt, String? caption, @JsonKey(name: 'aspect_ratio') String? aspectRatio, List<RichAction>? actions, @JsonKey(name: 'on_tap_callback') OnTapCallback? onTapCallback) image,required TResult Function( String id, List<RichButton> buttons, String layout) buttonGroup,required TResult Function( String id, List<RichSwatch> swatches, int columns) swatchGrid,required TResult Function( String id, @JsonKey(name: 'slot_id') String slotId, Map<String, dynamic>? payload) hostSlot,}) {final _that = this;
switch (_that) {
case TextComponent():
return text(_that.content,_that.format);case CardComponent():
return card(_that.id,_that.title,_that.image,_that.subtitle,_that.description,_that.metadata,_that.actions);case CarouselComponent():
return carousel(_that.id,_that.items,_that.config);case ProductListComponent():
return productList(_that.id,_that.items,_that.config);case ImageComponent():
return image(_that.id,_that.url,_that.alt,_that.caption,_that.aspectRatio,_that.actions,_that.onTapCallback);case ButtonGroupComponent():
return buttonGroup(_that.id,_that.buttons,_that.layout);case SwatchGridComponent():
return swatchGrid(_that.id,_that.swatches,_that.columns);case HostSlotComponent():
return hostSlot(_that.id,_that.slotId,_that.payload);}
}