setState method

  1. @protected
void setState(
  1. S next, {
  2. bool notify = true,
  3. bool forced = false,
  4. bool equalityCheck(
    1. S curr,
    2. S next
    )?,
})

Updates the state with the given next state.

next is the new state value to set. notify (default: true): Controls whether listeners are notified and the UI is rebuilt. forced (default: false): Forces the state update even if the value hasn't changed, useful for mutating iterable state. equalityCheck (optional): Custom function to determine if state has changed.

This method provides fine-grained control over state updates and listener notifications. By default, it uses basic equality checking (== and identical) to determine if the state has changed.

Example:

void increment() => setState(state + 1);

void updateSilently(int newValue) {
  setState(newValue, notify: false); // Update without notifying listeners
}

void forceUpdate(int newValue) {
  setState(newValue, forced: true); // Updates the state bypassing the equality check
}

void customEqualityUpdate(int newValue) {
  setState(newValue, equalityCheck: (curr, next) => curr.abs() == next.abs());
}

Implementation

@protected
void setState(
  S next, {
  bool notify = true,
  bool forced = false,
  bool Function(S curr, S next)? equalityCheck,
}) {
  _setState(
    next,
    notify: notify,
    forced: forced,
    onUpdate: onUpdate,
    equalityCheck: equalityCheck,
  );
}