dispose method

  1. @override
void dispose ()
override

Called when this object is removed from the tree permanently.

The framework calls this method when this State object will never build again. After the framework calls dispose, the State object is considered unmounted and the mounted property is false. It is an error to call setState at this point. This stage of the lifecycle is terminal: there is no way to remount a State object that has been disposed.

Subclasses should override this method to release any resources retained by this object (e.g., stop any active animations).

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then the State should subscribe to that object during initState, unsubscribe from the old object and subscribe to the new object when it changes in didUpdateWidget, and then unsubscribe from the object in dispose.

If you override this, make sure to end your method with a call to super.dispose().

See also deactivate, which is called prior to dispose.

Implementation

@override
void dispose() {
  assert(!_debugLocked);
  assert(() { _debugLocked = true; return true; }());
  for (NavigatorObserver observer in widget.observers)
    observer._navigator = null;
  final List<Route<dynamic>> doomed = _poppedRoutes.toList()..addAll(_history);
  for (Route<dynamic> route in doomed)
    route.dispose();
  _poppedRoutes.clear();
  _history.clear();
  focusScopeNode.detach();
  super.dispose();
  assert(() { _debugLocked = false; return true; }());
}