reduce method Null safety

CubixState<TState> reduce(
  1. {TState state(
    1. TState state
    )?,
  2. Dispatcher? remove,
  3. Dispatcher? add,
  4. List<Dispatcher>? dispatchers}
)

Implementation

CubixState<TState> reduce({
  TState Function(TState state)? state,
  Dispatcher? remove,
  Dispatcher? add,
  List<Dispatcher>? dispatchers,
}) {
  if (state == null && remove == null && add == null && dispatchers == null) {
    return this;
  }
  dispatchers ??= this.dispatchers;
  var nextDispatchers = dispatchers;
  if (add != null) {
    if (nextDispatchers == dispatchers) {
      nextDispatchers = [...dispatchers];
    }
    nextDispatchers.add(add);
  }
  if (remove != null && nextDispatchers.contains(remove)) {
    if (nextDispatchers == dispatchers) {
      nextDispatchers = [...dispatchers];
    }
    nextDispatchers.remove(remove);
  }
  final nextState = state == null ? this.state : state(this.state);

  if (nextState == this.state && nextDispatchers == dispatchers) {
    return this;
  }

  return CubixState(
    nextState,
    nextDispatchers,
  );
}