buildState method

  1. @internal
void buildState()

Invokes create and handles errors.

Implementation

@internal
void buildState() {
  ProviderElementBase? debugPreviouslyBuildingElement;
  final previousDidChangeDependency = _didChangeDependency;
  _didChangeDependency = false;
  assert(
    () {
      debugPreviouslyBuildingElement = _debugCurrentlyBuildingElement;
      _debugCurrentlyBuildingElement = this;
      return true;
    }(),
    '',
  );
  _didBuild = false;
  try {
    // TODO move outside this function?
    _mounted = true;
    create(didChangeDependency: previousDidChangeDependency);
  } catch (err, stack) {
    assert(
      () {
        _debugDidSetState = true;
        return true;
      }(),
      '',
    );
    _state = Result.error(err, stack);
  } finally {
    _didBuild = true;
    assert(
      () {
        _debugCurrentlyBuildingElement = debugPreviouslyBuildingElement;
        return true;
      }(),
      '',
    );

    assert(
      getState() != null,
      'Bad state, the provider did not initialize. Did "create" forget to set the state?',
    );
  }
}