New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
what about make context to be global variable instead of parameters? #65727
Comments
Many contexts exist at once (there is one for every widget in the tree) so just making it global is not feasible, what use case do you have in mind specifically? |
You can read more about BuildContext at https://api.flutter.dev/flutter/widgets/BuildContext-class.html. |
look at this links :
|
@mahmoudsalah37 For example to navigate you need a context that is a descendant of a To navigate you can avoid context by using a global navigatorKey/NavigatorState as described at https://stackoverflow.com/a/53397266. To show snack bars you can always create a global variable of ScaffoldState and use it to show it. Keep in mind that if you use it in a point where the final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
body: Center(
child: RaisedButton(
onPressed: () {
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text('Hi!'),
));
},
),
),
);
}
} @mahmoudsalah37 Does this helps? |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
I think it will be easy and help developers to use context anywhere without pass it as parameters.
The text was updated successfully, but these errors were encountered: