attachToRenderTree method Null safety

RenderObjectToWidgetElement<T> attachToRenderTree(
  1. BuildOwner owner,
  2. [RenderObjectToWidgetElement<T>? element]
)

Inflate this widget and actually set the resulting RenderObject as the child of container.

If element is null, this function will create a new element. Otherwise, the given element will have an update scheduled to switch to this widget.

Used by runApp to bootstrap applications.

Implementation

RenderObjectToWidgetElement<T> attachToRenderTree(BuildOwner owner, [ RenderObjectToWidgetElement<T>? element ]) {
  if (element == null) {
    owner.lockState(() {
      element = createElement();
      assert(element != null);
      element!.assignOwner(owner);
    });
    owner.buildScope(element!, () {
      element!.mount(null, null);
    });
  } else {
    element._newWidget = this;
    element.markNeedsBuild();
  }
  return element!;
}