unmount method Null safety

  1. @mustCallSuper
void unmount()

Transition from the "inactive" to the "defunct" lifecycle state.

Called when the framework determines that an inactive element will never be reactivated. At the end of each animation frame, the framework calls unmount on any remaining inactive elements, preventing inactive elements from remaining inactive for longer than a single animation frame.

After this function is called, the element will not be incorporated into the tree again.

Any resources this element holds should be released at this point. For example, RenderObjectElement.unmount calls RenderObject.dispose and nulls out its reference to the render object.

See the lifecycle documentation for Element for additional information.

Implementations of this method should end with a call to the inherited method, as in super.unmount().

Implementation

@mustCallSuper
void unmount() {
  assert(_lifecycleState == _ElementLifecycle.inactive);
  assert(_widget != null); // Use the private property to avoid a CastError during hot reload.
  assert(depth != null);
  assert(owner != null);
  // Use the private property to avoid a CastError during hot reload.
  final Key? key = _widget!.key;
  if (key is GlobalKey) {
    owner!._unregisterGlobalKey(key, this);
  }
  // Release resources to reduce the severity of memory leaks caused by
  // defunct, but accidentally retained Elements.
  _widget = null;
  _dependencies = null;
  _lifecycleState = _ElementLifecycle.defunct;
}