whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. Map<String, dynamic>? transport,
    2. Map<String, String>? env,
    3. LogConfig? log,
    4. RouterConfig? routing,
    5. DNSConfig? dns,
    6. List<InboundDetourConfig>? inbounds,
    7. List<OutboundDetourConfig>? outbounds,
    8. PolicyConfig? policy,
    9. APIConfig? api,
    10. MetricsConfig? metrics,
    11. StatsConfig? stats,
    12. ReverseConfig? reverse,
    13. @JsonKey.new(name: 'fakeDns') FakeDNSConfig? fakeDns,
    14. ObservatoryConfig? observatory,
    15. BurstObservatoryConfig? burstObservatory,
    16. VersionConfig? version,
    17. GeodataConfig? geodata,
    )?
)

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(
          Map<String, dynamic>? transport,
          Map<String, String>? env,
          LogConfig? log,
          RouterConfig? routing,
          DNSConfig? dns,
          List<InboundDetourConfig>? inbounds,
          List<OutboundDetourConfig>? outbounds,
          PolicyConfig? policy,
          APIConfig? api,
          MetricsConfig? metrics,
          StatsConfig? stats,
          ReverseConfig? reverse,
          @JsonKey(name: 'fakeDns') FakeDNSConfig? fakeDns,
          ObservatoryConfig? observatory,
          BurstObservatoryConfig? burstObservatory,
          VersionConfig? version,
          GeodataConfig? geodata)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _XrayConfig() when $default != null:
      return $default(
          _that.transport,
          _that.env,
          _that.log,
          _that.routing,
          _that.dns,
          _that.inbounds,
          _that.outbounds,
          _that.policy,
          _that.api,
          _that.metrics,
          _that.stats,
          _that.reverse,
          _that.fakeDns,
          _that.observatory,
          _that.burstObservatory,
          _that.version,
          _that.geodata);
    case _:
      return null;
  }
}