Conversation
The error thrown when dependOnInheritedWidgetOfExactType() is called before initState() completes said that "the rebuilt dependent widget will not reflect the changes in the inherited widget", which is not what happens: the widget is rebuilt and its build() method does see the new value. What is never updated is whatever initState() read, because initState() runs once and is not run again when a dependency changes. State.initState's documentation already said not to use dependOnInheritedWidgetOfExactType there, but did not say why.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the documentation and error messages in 'framework.dart' to clarify that 'initState' is called only once per 'State' object and is not re-run when an inherited widget changes, meaning values read within it are never updated. It also updates 'inherited_test.dart' to include a regression test verifying that the error message thrown when accessing an inherited widget in 'initState' correctly explains this behavior. There are no review comments, and thus no feedback is provided.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
dependOnInheritedWidgetOfExactType()is called beforeinitState()has completed, the error explains the rule like this:That last sentence describes something that does not happen. The dependency is registered either way, so the dependent widget is rebuilt and its
build()method does see the new value. What is never updated is whateverinitState()read, because the framework callsinitState()once perStateobject and does not call it again for those rebuilds. Reading the error as written, it is easy to conclude that depending on an inherited widget ininitState()breaks the rebuild itself, which is what the issue reports as confusing.This replaces that sentence with the actual reason, and adds the same reason to
State.initState's documentation, which told readers not to calldependOnInheritedWidgetOfExactType()there but not why. TheErrorHintthat points atbuild()anddidChangeDependencies()is unchanged.After:
The existing test for this error (
initState() dependency on Inherited asserts) only checks that something was thrown, so the message itself was untested; the new test covers it.ExpectFailnow forwards the caught error so a test can inspect it.The issue also suggests splitting
_StateLifecycle.createdinto two states so the message can distinguish "in the constructor" from "inside initState()". I left that out: it is a debug-only bookkeeping change and the summary line already names the method it happened before.Fixes #105705
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.