You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apps regularly want to respond to a stream of events that might be coming from non-UI related sources (such as network or platform channels). Common responses to such events might include:
Navigating to a new page.
Showing a dialog.
Showing a snackbar.
Such events are typically not tied to the lifecycle of the "current page". The BuildContext of the code that invokes the action could be long gone when the action finally resolves.
A few examples:
final value =awaitpermissionPluginCall();
Navigator.of(context).pushNamed(‘xxx’);
try {
final value =awaitsomeRpcCall();
} catch (e, st) {
Scaffold.of(context).showSnackBar(...)
}
There are solutions around some of these problems such as using NavigatorKey however, I have not seen a canonical way to grab a valid (aka current) context from anywhere in the app.
The ideal outcome of this issue would be to have a pattern similar to InheritedWidget.of(context) which locates the current context for you. Something like InheritedWidget.current.
Workarounds I have seen are pretty terrible:
Passing BuildContext around.
Passing State around.
Using APIs such as context.findRenderObject().attached to determine whether context is still valid.
Apps regularly want to respond to a stream of events that might be coming from non-UI related sources (such as network or platform channels). Common responses to such events might include:
Such events are typically not tied to the lifecycle of the "current page". The
BuildContextof the code that invokes the action could be long gone when the action finally resolves.A few examples:
There are solutions around some of these problems such as using NavigatorKey however, I have not seen a canonical way to grab a valid (aka current) context from anywhere in the app.
The ideal outcome of this issue would be to have a pattern similar to
InheritedWidget.of(context)which locates the current context for you. Something likeInheritedWidget.current.Workarounds I have seen are pretty terrible:
BuildContextaround.Statearound.context.findRenderObject().attachedto determine whether context is still valid.