mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? premium(
    1. ChatBoostSourcePremium value
    )?,
  2. TResult? giftCode(
    1. ChatBoostSourceGiftCode value
    )?,
  3. TResult? giveaway(
    1. ChatBoostSourceGiveaway 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(ChatBoostSourcePremium value)? premium,
  TResult? Function(ChatBoostSourceGiftCode value)? giftCode,
  TResult? Function(ChatBoostSourceGiveaway value)? giveaway,
}) {
  final _that = this;
  switch (_that) {
    case ChatBoostSourcePremium() when premium != null:
      return premium(_that);
    case ChatBoostSourceGiftCode() when giftCode != null:
      return giftCode(_that);
    case ChatBoostSourceGiveaway() when giveaway != null:
      return giveaway(_that);
    case _:
      return null;
  }
}