deactivate method
Let the StateX simply be re-created The framework calls this method whenever it removes this State object from the tree.
Implementation
// StateX? _oldStateX;
//
// //
// void _copyOverState() {
// // Nothing to copy over
// if (_oldStateX == null) {
// return;
// }
// // Local variable
// final oldStateX = _oldStateX!;
//
// // If the previous State was 'resumed'. May want to recover further??
// if (oldStateX._hadSystemEvent) {
// // Reset so not to cause any side-affects.
// oldStateX._hadSystemEvent = false;
// // If a different object and the same type. (Thought because it was being recreated, but not the case. gp)
// // if (this != oldStateX && runtimeType == oldStateX.runtimeType) {
//
// var copyOver = this != oldStateX;
//
// if (copyOver && controller != null && oldStateX.controller != null) {
// copyOver = controller!.identifier == oldStateX.controller!.identifier;
// }
//
// if (copyOver) {
// copyOver = controller.runtimeType == oldStateX.runtimeType;
// }
//
// if (copyOver) {
// // Copy over certain properties
// _copyOverStateFuture(oldStateX);
// _copyOverStateControllers(oldStateX);
// _copyOverStateException(oldStateX);
// _copyOverStateDependencies(oldStateX);
// updateNewStateX(oldStateX);
//
// assert(() {
// if (kDebugMode) {
// debugPrint('============ _copyOverState(): $this copied $oldStateX');
// }
// return true;
// }());
//
// // Testing Flutter lifecycle operation
// assert(() {
// if (oldStateX.resumedAppLifecycle || oldStateX.deactivated) {
// if (kDebugMode) {
// print(
// '============ _copyOverState(): resumed: ${oldStateX.resumedAppLifecycle} deactivated: ${oldStateX.deactivated}');
// }
// }
// return true;
// }());
// }
// }
// // cleanup
// _oldStateX = null;
// }
/// 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.
/// Users may have explicitly call this.
if (_deactivated) {
return;
}
// Indicate this State object is deactivated.
_deactivated = true;
/// Ignore Route changes
RouteObserverStates.unsubscribeRoutes(this);
/// If 'AppState' is not used
if (rootState == null) {
// Unregisters the given observer.
WidgetsBinding.instance.removeObserver(this);
}
// No 'setState()' functions are allowed to fully function at this point.
// _setStateAllowed = false;
for (final con in controllerList) {
//
con.deactivate();
// Pop the State object from the controller
con._popStateFromSetter(this);
}
super.deactivate();
// Remove from the list of StateX objects present in the app!
// I know! I know! It may be premature but Controllers still have access.
_removeFromMapOfStates(this);
_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;
assert(() {
if (_printEvents) {
debugPrint('============ Event: deactivate() in $this');
}
return true;
}());
}