whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  1. TResult? str(
    1. String field0
    )?,
  2. TResult? int(
    1. PlatformInt64 field0
    )?,
  3. TResult? float(
    1. double field0
    )?,
  4. TResult? bool(
    1. bool field0
    )?,
  5. TResult? listStr(
    1. List<String> field0
    )?,
  6. TResult? listInt(
    1. Int64List 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(String field0)? str,
  TResult? Function(PlatformInt64 field0)? int,
  TResult? Function(double field0)? float,
  TResult? Function(bool field0)? bool,
  TResult? Function(List<String> field0)? listStr,
  TResult? Function(Int64List field0)? listInt,
}) {
  final _that = this;
  switch (_that) {
    case PyArgument_Str() when str != null:
      return str(_that.field0);
    case PyArgument_Int() when int != null:
      return int(_that.field0);
    case PyArgument_Float() when float != null:
      return float(_that.field0);
    case PyArgument_Bool() when bool != null:
      return bool(_that.field0);
    case PyArgument_ListStr() when listStr != null:
      return listStr(_that.field0);
    case PyArgument_ListInt() when listInt != null:
      return listInt(_that.field0);
    case _:
      return null;
  }
}