deactivate method

  1. @override
  2. @mustCallSuper
void deactivate()
inherited

Called with every StateX associated with this Controller Initialize any 'time-consuming' operations at the beginning. Implement any asynchronous operations needed done at start up. initAsync() has failed and a 'error' widget instead will be displayed. This takes in the snapshot.error details. The framework calls this method whenever it removes this State object from the tree.

Implementation

// Save on function calls
// @override
// Future<bool> initAsyncState(covariant State state) async {
//   // Optionally call super for debugPrint()
//   super.initAsyncState(state);
//   return true;
// }

/// initAsync() has failed and a 'error' widget instead will be displayed.
/// This takes in the snapshot.error details.
// Save on function calls
// @override
// void onAsyncError(FlutterErrorDetails details) {
//   // Optionally call super for debugPrint()
//   super.onAsyncError(details);
// }

/// The framework calls this method whenever it removes this [State] object
/// from the tree.
@override
@mustCallSuper
void deactivate() {
  /// The framework calls this method whenever it removes this [State] object
  /// from the tree. Subclasses should override this method to clean up any links between
  /// this object and other elements in the tree.
  //
  if (!_deactivated) {
    try {
      // runZonedGuarded<void>(() {
      // scheduleMicrotask(() {

      // Ignore Route changes
      RouteObserverStates.unsubscribeRoutes(this);

      // Users may have explicitly call this.
      if (_deactivated) {
        return;
      }

      // Indicate this State object is deactivated.
      _deactivated = true;

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

      for (final con in controllerList) {
        con.deactivateState(this);
        // Pop the State object from the controller
        con._popStateFromSetter(this);
        if (con.lastState == null) {
          con.deactivate();
        }
      }

      _setStateAllowed = true;

      // In some cases, if then reinserted back in another part of the tree
      // the build is called, and so setState() is not necessary.
      _setStateRequested = false;

      super.deactivate();
      // });
    } catch (e) {
      // }, (error, stackTrace) {
      // Record error in device's log
      _logPackageError(
        e,
        library: 'part01_statex.dart',
        description: 'Error in deactivate()',
      );
      // });
    }
  }
}