whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. String typeName,
    2. String? typeFullLibraryName,
    3. String name,
    4. bool isDeprecated,
    5. bool isStatic,
    6. bool isConst,
    7. bool isExperimental,
    8. Set<String>? entryPoints,
    9. String relativePath,
    10. bool isReadable,
    11. bool isWriteable,
    )?
)

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 typeName,
          String? typeFullLibraryName,
          String name,
          bool isDeprecated,
          bool isStatic,
          bool isConst,
          bool isExperimental,
          Set<String>? entryPoints,
          String relativePath,
          bool isReadable,
          bool isWriteable)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _FieldDeclaration() when $default != null:
      return $default(
          _that.typeName,
          _that.typeFullLibraryName,
          _that.name,
          _that.isDeprecated,
          _that.isStatic,
          _that.isConst,
          _that.isExperimental,
          _that.entryPoints,
          _that.relativePath,
          _that.isReadable,
          _that.isWriteable);
    case _:
      return null;
  }
}