Skip to content

Confusing error message : dependOnInheritedElement() was called before ${state.runtimeType}.initState() #105705

Closed

Description

@Stitch-Taotao

Confusing error message is :

'When an inherited widget changes, for example if the value of Theme.of() changes, '
"its dependent widgets are rebuilt. If the dependent widget's reference to "
'the inherited widget is in a constructor or an initState() method, '
'then the rebuilt dependent widget will not reflect the changes in the '
'inherited widget.'

image

In fact , when similar usage of inherited widget (example: Theme.of()) is in constructor indeed will not reflect the changes .
when in initState()indeed will reflect the changes .

Cause,state.initState() and state.didChangeDependencies() are both in _firstBuild which indeed after super.mount when element first mount, So "If the dependent widget's reference to the inherited widget is in an initState() method then the rebuilt dependent widget will reflect the changes in the inherited widget"

OverAll , I think the _StateLifecycle should make a litte change :

Before chagne :
image
After chagne :

enum _StateLifecycle {
  /// The [State] object has been created. [State.initState] is called at this
  /// time.
  created,

  /// ------- Add -----------
  beforeInitialized;

  /// The [State.initState] method has been called but the [State] object is
  /// not yet ready to build. [State.didChangeDependencies] is called at this time.
  initialized,

  /// The [State] object is ready to build and [State.dispose] has not yet been
  /// called.
  ready,

  /// The [State.dispose] method has been called and the [State] object is
  /// no longer able to build.
  defunct,
}

_firstBuild code flow should be :

_firstBuild {
  assert(state._debugLifecycleState == _StateLifecycle.created);
  assert(() {
      state._debugLifecycleState = _StateLifecycle.beforeInitialized;
      return true;
    }());
 ...
  state.initState();
 ...
  assert(() {
      state._debugLifecycleState = _StateLifecycle.initialized;
      return true;
    }());
 ...
    state.didChangeDependencies();
 ...
    assert(() {
      state._debugLifecycleState = _StateLifecycle.ready;
      return true;
    }());
 ...
}

Activity

  1. maheshj01 commented on Jun 10, 2022

    @maheshj01
    Member

    Hi @Stitch-Taotao, Thanks for filing the issue. Labeling this issue for further insights from the team

  2. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    d: api docsIssues with https://api.flutter.dev/
    and removed
    in triagePresently being triaged by the triage team
    on Jun 10, 2022
  3. added
    a: error messageError messages from the Flutter framework
    P2Important issues not at the top of the work list
    on Jun 16, 2022
  4. github-actions commented on Oct 27, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  5. locked as resolved and limited conversation to collaborators on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: error messageError messages from the Flutter frameworkd: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions