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,
    'This element is no longer in its initial state (${_lifecycleState.name})',
  );
  assert(
    _parent == null,
    "This element already has a parent ($_parent) and it shouldn't have one yet.",
  );
  assert(
    parent == null || parent._lifecycleState == _ElementLifecycle.active,
    'Parent ($parent) should be null or in the active state (${parent._lifecycleState.name})',
  );
  assert(
    slot == null,
    "This element already has a slot ($slot) and it shouldn't",
  );
  _parent = parent;
  _slot = newSlot;
  _lifecycleState = _ElementLifecycle.active;
  _depth = 1 + (_parent?.depth ?? 0);
  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;
    _parentBuildScope = parent.buildScope;
  }
  assert(owner != null);
  final Key? key = widget.key;
  if (key is GlobalKey) {
    owner!._registerGlobalKey(key, this);
  }
  _updateInheritance();
  attachNotificationTree();
}