setState method

void setState(
  1. VoidCallback callback
)

Implementation

void setState(VoidCallback callback) {
  callback.call();

  if (!_ready) return;
  if (context.widget == null) {
    throw Exception(
        "Tried to call setState on a $runtimeType Widget which has not been rendered yet!");
  }
  if (!mounted) {
    throw Exception(
        "Tried to call setState on a $runtimeType Widget which is not mounted!");
  } else if (context.child?.element?.isConnected != true) {
    throw Exception(
        "Tried to call setState on a $runtimeType Widget while the child is not connected!");
  }

  // Create a local only context where the callback list is overrided
  final oldCallBacks = context.callbacks;
  context = context.overrideCallbacks();

  final child = build(context);

  final childContext =
      (context.child?.widget.runtimeType == child.runtimeType &&
              context.child != null)
          ? context.child!
          : context.createChildContext(
              copyOldProperties: false,
              copyOldElement:
                  context.child?.widget.runtimeType == child.runtimeType);

  // we reuse the same context so we know what was the previous context to
  // reuse the same widgets
  // override callbacks
  context.child!.callbacks = context.callbacks;

  // Import back the function local context to the class local context.
  // This was needed to override the callbacks list.
  child.inflate(childContext);

  render();

  assert(childContext.element?.isConnected == true);

  context.executeCallbacks();
  context.callbacks = oldCallBacks;
}