Skip to content

Prevent looking up inherited widget values from initState() - #5609

Merged
HansMuller merged 5 commits into
flutter:masterfrom
HansMuller:inherited_widget_initstate
Aug 26, 2016
Merged

HansMuller merged 5 commits into
flutter:masterfrom
HansMuller:inherited_widget_initstate

Conversation

@HansMuller

Copy link
Copy Markdown
Contributor

Looking up an inherited widget with BuildContext.inheritFromWidgetOfExactType() from an initState() method now causes an assert.

Static methods like Theme.of() use inheritFromWidgetOfExactType(). Using it at initState() time is ill-advised because updates to the inherited widget only cause the dependent widget to rebuild. Initializations based on the inherited widget that occur in initState() will not rerun when the widget is rebuilt.

There were a handful of cases like this in the code base.

Fixes #5491

@HansMuller

Copy link
Copy Markdown
Contributor Author

cc @Hixie

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous space

@Hixie

Hixie commented Aug 26, 2016

Copy link
Copy Markdown
Contributor

Let's talk tomorrow about how to still enable this for the rare cases where it's done right for a good reason...

@HansMuller
HansMuller force-pushed the inherited_widget_initstate branch from f25f0f2 to a0971ad Compare August 26, 2016 17:39
// changes to the inherited widgets that _resolveImage depends on, notably
// DefaultAssetBundle, are handle by the dependenciesChanged() method. We're
// doing it here instead to avoid the assert that disallows references to
// inherited widgets at initState() time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"...because we have found that asserts catches bugs frequently, and is therefore worth this minor inconvenience here."

// DefaultAssetBundle, are handle by the dependenciesChanged() method. We're
// doing it here instead to avoid the assert that disallows references to
// inherited widgets at initState() time. We've found that assert to be a reliable
// source of real bugs, and is therefore worth this minor inconvenience here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitty nit nit: oops, my grammar was bad. "and that it is" rather than "and is".

@Hixie

Hixie commented Aug 26, 2016

Copy link
Copy Markdown
Contributor

LGTM

@HansMuller
HansMuller merged commit d0e72d6 into flutter:master Aug 26, 2016
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inherit from widget should throw an exception in initState

2 participants