mapOrNull<TResult extends Object?> method

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