dispose method

  1. @protected
  2. @override
  3. @mustCallSuper
void dispose()
inherited

The framework calls this method when this StateMVC object will never build again and will be disposed of.

Implementation

@protected
@override
@mustCallSuper
void dispose() {
  /// The State object's lifecycle is terminated.
  /// Subclasses should override this method to release any resources retained
  /// by this object (e.g., stop any active animations).

  // No 'setState()' functions are allowed to fully function at this point.
  _rebuildAllowed = false;

  for (final listener in _beforeList) {
    listener.dispose();
  }
  for (final con in _controllerList) {
    // This state's association is severed.
    con._popState(this);

    // Don't call its dispose if it's in other State objects.
    if (con._stateMVCSet.isEmpty) {
      con.dispose();
    }
  }
  _disposeControllerListing();
  for (final listener in _afterList) {
    listener.dispose();
  }
  _disposeStateEventList();

  // *In some cases, the setState() will be called again! gp
  _rebuildAllowed = true;

  // Unregisters the given observer.
  WidgetsBinding.instance.removeObserver(this);

  // Remove any 'Controller' reference
  _controller = null;

  // Clear the list of Controllers.
  _cons.clear();

  // Return the original error routine.
  FlutterError.onError = currentErrorFunc;

  // Remove the State object from a collection.
  rootState?._removeStateMVC(this);

  super.dispose();
}