Repository navigation
Commit d2e5f21
authored
Explain why initState() is too early to depend on an inherited widget (flutter#192727)
When `dependOnInheritedWidgetOfExactType()` is called before
`initState()` has completed, the error explains the rule like this:
> 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.
That last sentence is easy to read as saying the rebuild itself does not
see the change, which is what the issue reports as confusing. The
rebuild does see it: a widget that depends on an inherited widget is
rebuilt when that inherited widget changes, and a `build()` method that
reads it gets the new value. What is never updated is whatever
`initState()` read, because the framework calls `initState()` only once
for each `State` object and does not call it again for those rebuilds.
This replaces that sentence with the actual reason, and adds the same
reason to `State.initState`'s documentation, which told readers not to
call `dependOnInheritedWidgetOfExactType()` there but not why. The
`ErrorHint` that points at `build()` and `didChangeDependencies()` is
unchanged.
After:
> When an inherited widget changes, for example if the value of
Theme.of() changes, its dependent widgets are rebuilt. The framework
calls initState() only once for each State object and does not call it
again for those rebuilds, so a value that initState() reads from an
inherited widget is never updated when that inherited widget changes.
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. `ExpectFail` now forwards the
caught error so a test can inspect it.
The issue also suggests adding a `_StateLifecycle` value between
`created` and `initialized`, set just before `initState()` is called. I
left that out. A `State` only gets its context after it is constructed
(`state._element` is assigned in the `StatefulElement` constructor after
`createState()` returns), so the only `State` code that runs with a
context before `initState()` completes is `initState()` itself, and a
new value would have no constructor case to separate. That is also why
the new message no longer mentions constructors; the general advice
about constructors stays in the
`BuildContext.dependOnInheritedWidgetOfExactType` docs.
Fixes flutter#105705
## Pre-launch Checklist
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
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](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot 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.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[flutter/website]: https://github.com/flutter/website
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md1 parent dd5c509 commit d2e5f21
2 files changed
Lines changed: 29 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
999 | 999 | | |
1000 | 1000 | | |
1001 | 1001 | | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
1005 | 1007 | | |
1006 | 1008 | | |
1007 | 1009 | | |
| |||
6075 | 6077 | | |
6076 | 6078 | | |
6077 | 6079 | | |
6078 | | - | |
6079 | | - | |
6080 | | - | |
6081 | | - | |
| 6080 | + | |
| 6081 | + | |
| 6082 | + | |
| 6083 | + | |
6082 | 6084 | | |
6083 | 6085 | | |
6084 | 6086 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
532 | | - | |
| 532 | + | |
| 533 | + | |
533 | 534 | | |
534 | 535 | | |
535 | | - | |
536 | | - | |
| 536 | + | |
| 537 | + | |
537 | 538 | | |
538 | 539 | | |
539 | 540 | | |
540 | 541 | | |
541 | | - | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
542 | 556 | | |
543 | 557 | | |
544 | 558 | | |
| |||
0 commit comments