clear method

void clear({
  1. bool force = false,
  2. bool removeFromParent = false,
})

Clears the component, removing the last rendered content.

  • If force is true performs a clear operation even if isRendered returns false.
  • If removeFromParent is true it will also remove the content element from the parent.

Implementation

void clear({bool force = false, bool removeFromParent = false}) {
  if (!force && !isRendered) return;

  if (!force && preserveRender) {
    // When any UIAsyncContent is loading, it relies on the previously
    // rendered elements to attach the newly loaded content, replacing
    // the old nodes. Clearing the component elements here would break
    // that attachment process.
    if (isLoadingUIAsyncContent) {
      return;
    }
  }

  if (_renderedElements != null) {
    for (var e in _renderedElements!) {
      if (e is UIComponent) {
        e.delete();
      } else if (e.isElement) {
        (e as Element).remove();
      }
    }

    _renderedElements = null;
  }

  _renderedAsyncContents?.clear();
  _renderedAsyncContents = null;

  var content = _content;

  if (content != null) {
    // Removes all the child nodes at once (`innerHTML = ''`):
    content.clear();

    if (removeFromParent) {
      content.remove();
    }
  }

  _rendered = false;
}