setState method

  1. @protected
void setState (VoidCallback fn)
@protected, inherited

Schedule a call to buildTransitions.

Whenever you need to change internal state for a ModalRoute object, make the change in a function that you pass to setState, as in:

setState(() { myState = newValue });

If you just change the state directly without calling setState, then the route will not be scheduled for rebuilding, meaning that its rendering will not be updated.

Implementation

@protected
void setState(VoidCallback fn) {
  if (_scopeKey.currentState != null) {
    _scopeKey.currentState._routeSetState(fn);
  } else {
    // The route isn't currently visible, so we don't have to call its setState
    // method, but we do still need to call the fn callback, otherwise the state
    // in the route won't be updated!
    fn();
  }
}