onMount method

  1. @override
  2. @mustCallSuper
void onMount()
override

Called when the component is added to its parent.

This method only runs when the component is fully loaded, i.e. after onLoad. However, onLoad only ever runs once for the component, whereas onMount runs every time the component is inserted into the game tree.

This method runs when the component is about to be added to its parent. At this point the parent property already holds a reference to this component's parent, however the parent doesn't have this component among its children yet.

After this method completes, the component is added to the parent's children set, and then the flag isMounted set to true.

Example:

@override
void onMount() {
  position = parent!.size / 2;
}

See also:

  • onRemove that is called every time the component is removed from the game tree

Implementation

@override
@mustCallSuper
void onMount() {
  super.onMount();
  assert(
    button != null,
    'The button has to either be passed in as an argument or set in onLoad',
  );
  if (!contains(button!)) {
    add(button!);
  }
}