build method

  1. @override
Widget build()
override

Subclasses should override this function to actually call the appropriate build function (e.g., StatelessWidget.build or State.build) for their widget.

Implementation

@override
Widget build() {
  // Check whether we can cancel the rebuild (caused by HookState.mayNeedRebuild).
  final mustRebuild = _isOptionalRebuild != true ||
      _shouldRebuildQueue.any((cb) => cb.value());

  _isOptionalRebuild = null;
  _shouldRebuildQueue.clear();

  if (!mustRebuild) {
    return _buildCache!;
  }

  if (kDebugMode) {
    _debugIsInitHook = false;
  }
  _currentHookState = _hooks.isEmpty ? null : _hooks.first;
  HookElement._currentHookElement = this;
  try {
    _buildCache = super.build();
  } finally {
    _isOptionalRebuild = null;
    _unmountAllRemainingHooks();
    HookElement._currentHookElement = null;
    if (_needDispose != null && _needDispose!.isNotEmpty) {
      for (_Entry<HookState<dynamic, Hook<dynamic>>>? toDispose =
              _needDispose!.last;
          toDispose != null;
          toDispose = toDispose.previous) {
        toDispose.value.dispose();
      }
      _needDispose = null;
    }
  }

  return _buildCache!;
}