whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  1. TResult? httpStub(
    1. String responseStubValue,
    2. int? statusCodeStubValue,
    3. Map<String, String> headersStubValue
    )?,
  2. TResult? genericStub(
    1. void setUp(
      1. dynamic params
      )
    )?,
})

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 responseStubValue, int? statusCodeStubValue,
          Map<String, String> headersStubValue)?
      httpStub,
  TResult? Function(void Function(dynamic params) setUp)? genericStub,
}) {
  final _that = this;
  switch (_that) {
    case _HttpStub() when httpStub != null:
      return httpStub(_that.responseStubValue, _that.statusCodeStubValue,
          _that.headersStubValue);
    case _GenericStub() when genericStub != null:
      return genericStub(_that.setUp);
    case _:
      return null;
  }
}