Skip to content
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

Closed
mahmoudsalah37 opened this issue Sep 13, 2020 · 5 comments
Closed
Labels
in triage Presently being triaged by the triage team

Comments

@mahmoudsalah37
Copy link

I think it will be easy and help developers to use context anywhere without pass it as parameters.

@pingbird
Copy link
Member

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?

@pedromassangocode
Copy link

You can read more about BuildContext at https://api.flutter.dev/flutter/widgets/BuildContext-class.html.

@pedromassangocode pedromassangocode added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Sep 14, 2020
@mahmoudsalah37
Copy link
Author

mahmoudsalah37 commented Sep 14, 2020

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?

look at this links :

  1. How to get context globally? #32304
  2. https://stackoverflow.com/questions/55956707/how-do-i-access-buildcontext-outside-of-a-stateful-or-stateless-widget
  3. https://stackoverflow.com/questions/51803755/getting-buildcontext-in-flutter-for-localization
  4. https://stackoverflow.com/questions/56144016/how-do-i-make-the-global-type-context-variable-in-flutter
    why i need everytime to use someting like navigator and snakbar (etc...) out of statefull ot stateless widget
    i must pass param of BuildContext.
    from
    Scaffold.of(context).showSnackBar(SnackBar( content: Text('Hello.') ));
    to
    Scaffold.showSnackBar(SnackBar( content: Text('Hello.') ));
    i think it will be so easy and speed performance

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 14, 2020
@pedromassangocode
Copy link

@mahmoudsalah37
You can think of a BuildContext as a reference of a Widget and depending on the use case, you may need a reference of different widgets for different tasks.

For example to navigate you need a context that is a descendant of a Navigator widget. To show a SnackBar you need a context that is a descendant of a Scaffold widget. Widgets in Flutter can change locations and when they change they may (or may not) be a child of a Scaffold widget and if then you try to show a SnackBar it may throw an exception because it needs a Scaffold widget to show a snackBar and the scaffold is not available anymore.

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 Scaffold is not in the tree you will not be able to display a snackBar:

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?
@PixelToast do you agree that this is not a valid proposal and should be closed?

@pedromassangocode pedromassangocode added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 14, 2020
@pedromassangocode pedromassangocode removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 21, 2020
@github-actions
Copy link

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 flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
in triage Presently being triaged by the triage team
Projects
None yet
Development

No branches or pull requests

3 participants