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

Context passing around in StatelessWidget #23400

Closed
GreenTigerEye opened this issue Oct 23, 2018 · 3 comments
Closed

Context passing around in StatelessWidget #23400

GreenTigerEye opened this issue Oct 23, 2018 · 3 comments

Comments

@GreenTigerEye
Copy link

GreenTigerEye commented Oct 23, 2018

Why do we have to pass the context around in a StatelessWidget?

Improvement Question: Wouldn't it be better to allow also in a StatlessWidget the context everywhere? Or do I miss some point?

Bad example! Context needs to be passed around in a StatelessWidget.

class ContextPassingAroundNeeded extends StatelessWidget {
  @override
  Widget build(BuildContext context) => method1(context);

  Widget method1(BuildContext context) {
    context.inheritFromWidgetOfExactType(Type); // whatever
    
    return method2(context);
  }

  Widget method2(BuildContext context) {
    context.inheritFromWidgetOfExactType(Object); // whatever
    return Container();
  }
}

In a StatefulWidget it is also possible without passing the context around, because the context variable is accessable everywhere in the state. It's nice however why isn't it also possible in a StatelessWidget?

class ContextPassingAroundNotNeeded extends StatefulWidget {
  @override
  _ContextPassingAroundNotNeededState createState() => _ContextPassingAroundNotNeededState();
}

class _ContextPassingAroundNotNeededState extends State<ContextPassingAroundNotNeeded> {
  @override
  Widget build(BuildContext context) => method1();

  Widget method1() {
    context.inheritFromWidgetOfExactType(Type); // whatever

    return method2();
  }

  Widget method2() {
    context.inheritFromWidgetOfExactType(Object); // whatever
    return Container();
  }
}
@GreenTigerEye GreenTigerEye changed the title Context in Stateless application Context passing around in StatelessWidget Oct 23, 2018
@zoechi
Copy link
Contributor

zoechi commented Oct 24, 2018

A stateless image is immutable, therefore no way to store the context anywhere when it becomes available.

@zoechi zoechi closed this as completed Oct 24, 2018
@ForzaMark
Copy link

I guess you would point out a stateless widget is immutable instead of an stateless image

@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 25, 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

No branches or pull requests

3 participants