whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. bool? abort,
    2. bool? assert_,
    3. bool? asyncHooks,
    4. bool? buffer,
    5. bool? childProcess,
    6. bool? console,
    7. bool? crypto,
    8. bool? dns,
    9. bool? events,
    10. bool? exceptions,
    11. bool? fetch,
    12. bool? fs,
    13. bool? navigator,
    14. bool? net,
    15. bool? os,
    16. bool? path,
    17. bool? perfHooks,
    18. bool? process,
    19. bool? streamWeb,
    20. bool? stringDecoder,
    21. bool? timers,
    22. bool? tty,
    23. bool? url,
    24. bool? util,
    25. bool? zlib,
    26. bool? json,
    )?
)

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(
          bool? abort,
          bool? assert_,
          bool? asyncHooks,
          bool? buffer,
          bool? childProcess,
          bool? console,
          bool? crypto,
          bool? dns,
          bool? events,
          bool? exceptions,
          bool? fetch,
          bool? fs,
          bool? navigator,
          bool? net,
          bool? os,
          bool? path,
          bool? perfHooks,
          bool? process,
          bool? streamWeb,
          bool? stringDecoder,
          bool? timers,
          bool? tty,
          bool? url,
          bool? util,
          bool? zlib,
          bool? json)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _JsBuiltinOptions() when $default != null:
      return $default(
          _that.abort,
          _that.assert_,
          _that.asyncHooks,
          _that.buffer,
          _that.childProcess,
          _that.console,
          _that.crypto,
          _that.dns,
          _that.events,
          _that.exceptions,
          _that.fetch,
          _that.fs,
          _that.navigator,
          _that.net,
          _that.os,
          _that.path,
          _that.perfHooks,
          _that.process,
          _that.streamWeb,
          _that.stringDecoder,
          _that.timers,
          _that.tty,
          _that.url,
          _that.util,
          _that.zlib,
          _that.json);
    case _:
      return null;
  }
}