whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  1. TResult? none()?,
  2. TResult? boolean(
    1. bool field0
    )?,
  3. TResult? integer(
    1. PlatformInt64 field0
    )?,
  4. TResult? float(
    1. double field0
    )?,
  5. TResult? bigint(
    1. String field0
    )?,
  6. TResult? string(
    1. String field0
    )?,
  7. TResult? bytes(
    1. Uint8List field0
    )?,
  8. TResult? array(
    1. List<JsValue> field0
    )?,
  9. TResult? object(
    1. Map<String, JsValue> field0
    )?,
  10. TResult? date(
    1. PlatformInt64 field0
    )?,
  11. TResult? symbol(
    1. String field0
    )?,
  12. TResult? function(
    1. String field0
    )?,
})

A variant of when that fallback to returning null

It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case _:
    return null;
}

Implementation

@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  TResult? Function()? none,
  TResult? Function(bool field0)? boolean,
  TResult? Function(PlatformInt64 field0)? integer,
  TResult? Function(double field0)? float,
  TResult? Function(String field0)? bigint,
  TResult? Function(String field0)? string,
  TResult? Function(Uint8List field0)? bytes,
  TResult? Function(List<JsValue> field0)? array,
  TResult? Function(Map<String, JsValue> field0)? object,
  TResult? Function(PlatformInt64 field0)? date,
  TResult? Function(String field0)? symbol,
  TResult? Function(String field0)? function,
}) {
  final _that = this;
  switch (_that) {
    case JsValue_None() when none != null:
      return none();
    case JsValue_Boolean() when boolean != null:
      return boolean(_that.field0);
    case JsValue_Integer() when integer != null:
      return integer(_that.field0);
    case JsValue_Float() when float != null:
      return float(_that.field0);
    case JsValue_Bigint() when bigint != null:
      return bigint(_that.field0);
    case JsValue_String() when string != null:
      return string(_that.field0);
    case JsValue_Bytes() when bytes != null:
      return bytes(_that.field0);
    case JsValue_Array() when array != null:
      return array(_that.field0);
    case JsValue_Object() when object != null:
      return object(_that.field0);
    case JsValue_Date() when date != null:
      return date(_that.field0);
    case JsValue_Symbol() when symbol != null:
      return symbol(_that.field0);
    case JsValue_Function() when function != null:
      return function(_that.field0);
    case _:
      return null;
  }
}