maybeWhen<TResult extends Object?> method

  1. @optionalTypeArgs
TResult maybeWhen<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,
    )?, {
  2. required TResult orElse(),
})

A variant of when that fallback to an orElse callback.

It is equivalent to doing:

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

Implementation

@optionalTypeArgs
TResult maybeWhen<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, {
  required TResult orElse(),
}) {
  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 orElse();
  }
}