mount method

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

Add this element to the tree in the given slot 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, visitChildren, RenderObjectElement.insertRenderObjectChild, RenderObjectElement.moveRenderObjectChild, and RenderObjectElement.removeRenderObjectChild.

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

Implementation

@mustCallSuper
void mount(Element? parent, Object? newSlot) {
  assert(_lifecycleState == _ElementLifecycle.initial);
  assert(_parent == null);
  assert(parent == null || parent._lifecycleState == _ElementLifecycle.active);
  assert(slot == null);
  _parent = parent;
  _slot = newSlot;
  _lifecycleState = _ElementLifecycle.active;
  _depth = _parent != null ? _parent!.depth + 1 : 1;
  if (parent != null) {
    // Only assign ownership if the parent is non-null. If parent is null
    // (the root node), the owner should have already been assigned.
    // See RootRenderObjectElement.assignOwner().
    _owner = parent.owner;
  }
  assert(owner != null);
  final Key? key = widget.key;
  if (key is GlobalKey) {
    owner!._registerGlobalKey(key, this);
  }
  _updateInheritance();
  attachNotificationTree();
}