rebuild method Null safety

void rebuild(
  1. {bool force = false}
)

Cause the widget to update itself. In debug builds, also verify various invariants.

Called by the BuildOwner when BuildOwner.scheduleBuildFor has been called to mark this element dirty, by mount when the element is first built, and by update when the widget has changed.

The method will only rebuild if dirty is true. To rebuild regardless of the dirty flag, set force to true. Forcing a rebuild is convenient from update, during which dirty is false.

Implementation

@pragma('vm:prefer-inline')
void rebuild({bool force = false}) {
  assert(_lifecycleState != _ElementLifecycle.initial);
  if (_lifecycleState != _ElementLifecycle.active || (!_dirty && !force)) {
    return;
  }
  assert(() {
    debugOnRebuildDirtyWidget?.call(this, _debugBuiltOnce);
    if (debugPrintRebuildDirtyWidgets) {
      if (!_debugBuiltOnce) {
        debugPrint('Building $this');
        _debugBuiltOnce = true;
      } else {
        debugPrint('Rebuilding $this');
      }
    }
    return true;
  }());
  assert(_lifecycleState == _ElementLifecycle.active);
  assert(owner!._debugStateLocked);
  Element? debugPreviousBuildTarget;
  assert(() {
    debugPreviousBuildTarget = owner!._debugCurrentBuildTarget;
    owner!._debugCurrentBuildTarget = this;
    return true;
  }());
  performRebuild();
  assert(() {
    assert(owner!._debugCurrentBuildTarget == this);
    owner!._debugCurrentBuildTarget = debugPreviousBuildTarget;
    return true;
  }());
  assert(!_dirty);
}