addToParent method

Future<void>? addToParent(
  1. Component parent
)

Adds this component to the provided parent (see add for details).

Implementation

Future<void>? addToParent(Component parent) {
  assert(
    _parent == null,
    '$this cannot be added to $parent because it already has a parent: '
    '$_parent',
  );
  assert(
    _state == LifecycleState.uninitialized ||
        _state == LifecycleState.removed,
  );
  _parent = parent;
  parent.lifecycle._children.add(this);

  if (!isLoaded) {
    final root = parent.findGame();
    if (root != null) {
      assert(
        root.hasLayout,
        'add() called before the game has a layout. Did you try to add '
        'components from the constructor? Use the onLoad() method instead.',
      );
      return _load();
    }
  }
  return null;
}