mount method

  1. @mustCallSuper
void mount(
  1. Element? parent,
  2. Element? prevSibling
)

Add this element to the tree as a child of the given parent.

The framework calls this function when a newly created element is added to the tree for the first time. Use this method to initialize state that depends on having a parent. State that is independent of the parent can more easily be initialized in the constructor.

This method transitions the element from the "initial" lifecycle state to the "active" lifecycle state.

Subclasses that override this method are likely to want to also override update and visitChildren.

Implementations of this method should start with a call to the inherited method, as in super.mount(parent).

Implementation

@mustCallSuper
void mount(Element? parent, Element? prevSibling) {
  assert(_lifecycleState == _ElementLifecycle.initial);
  assert(_component != null);
  assert(_parent == null);
  assert(parent == null || parent._lifecycleState == _ElementLifecycle.active);

  _parent = parent;
  _parentRenderObjectElement = parent is RenderObjectElement ? parent : parent?._parentRenderObjectElement;

  _prevSibling = prevSibling;
  _prevAncestorSibling = _prevSibling ?? (_parent is RenderObjectElement ? null : _parent?._prevAncestorSibling);

  _lifecycleState = _ElementLifecycle.active;
  _depth = parent != null ? parent.depth + 1 : 1;

  if (parent != null) {
    _owner = parent.owner;
    _binding = parent.binding;
  }
  assert(_owner != null);
  assert(_binding != null);

  final Key? key = component.key;
  if (key is GlobalKey) {
    ComponentsBinding._registerGlobalKey(key, this);
  }
  _updateInheritance();
  _updateObservers();
  attachNotificationTree();
}