Ask Your Question
2

Before _ScreenState.initState() finished, an error occurred where either inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was invoked.

asked 2022-02-26 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-09-06 16:00:00 +0000

plato gravatar image

This error occurs when a widget tries to access the state of a parent widget before the parent widget has finished initializing its state.

The initState() method is called when a stateful widget is first created, and it is where you can initialize any state or subscribe to any streams or future tasks needed for the widget. If you call inheritFromWidgetOfExactType() or inheritFromElement() before initState() has finished executing, it will throw this error.

To fix this error, make sure that you don't call any methods that depend on the state of a parent widget until after the parent widget has finished initializing its state. You can use the WidgetsBinding.instance.addPostFrameCallback() method to execute a callback function after the widget has finished building, which can then safely access the parent state.

Alternatively, you can move the code that depends on the parent state to a later lifecycle method like didChangeDependencies() or build() once the parent widget has finished building.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-26 11:00:00 +0000

Seen: 14 times

Last updated: Sep 06 '22