when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. String? ruleTag,
    2. String? outboundTag,
    3. String? balancerTag,
    4. XrayStringList? domain,
    5. XrayStringList? domains,
    6. XrayStringList? ip,
    7. XrayPortList? port,
    8. XrayNetworkList? network,
    9. XrayStringList? sourceIP,
    10. XrayStringList? source,
    11. XrayPortList? sourcePort,
    12. XrayStringList? user,
    13. XrayPortList? vlessRoute,
    14. XrayStringList? inboundTag,
    15. @JsonKey.new(name: 'protocol') XrayStringList? protocols,
    16. @JsonKey.new(name: 'attrs') Map<String, String>? attributes,
    17. XrayStringList? localIP,
    18. XrayPortList? localPort,
    19. XrayStringList? process,
    20. WebhookRuleConfig? webhook,
    )
)

A switch-like method, using callbacks.

As opposed to map, this offers destructuring. It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case Subclass2(:final field2):
    return ...;
}

Implementation

@optionalTypeArgs
TResult when<TResult extends Object?>(
  TResult Function(
          String? ruleTag,
          String? outboundTag,
          String? balancerTag,
          XrayStringList? domain,
          XrayStringList? domains,
          XrayStringList? ip,
          XrayPortList? port,
          XrayNetworkList? network,
          XrayStringList? sourceIP,
          XrayStringList? source,
          XrayPortList? sourcePort,
          XrayStringList? user,
          XrayPortList? vlessRoute,
          XrayStringList? inboundTag,
          @JsonKey(name: 'protocol') XrayStringList? protocols,
          @JsonKey(name: 'attrs') Map<String, String>? attributes,
          XrayStringList? localIP,
          XrayPortList? localPort,
          XrayStringList? process,
          WebhookRuleConfig? webhook)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _RouterRule():
      return $default(
          _that.ruleTag,
          _that.outboundTag,
          _that.balancerTag,
          _that.domain,
          _that.domains,
          _that.ip,
          _that.port,
          _that.network,
          _that.sourceIP,
          _that.source,
          _that.sourcePort,
          _that.user,
          _that.vlessRoute,
          _that.inboundTag,
          _that.protocols,
          _that.attributes,
          _that.localIP,
          _that.localPort,
          _that.process,
          _that.webhook);
    case _:
      throw StateError('Unexpected subclass');
  }
}