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

Under what circumstances would I want to attach my context to the InheritedWidget #1

Closed
larryq opened this issue Apr 25, 2019 · 2 comments

Comments

@larryq
Copy link

larryq commented Apr 25, 2019

My apologies, but opening an issue appears to be the only way I can ask a question. Your library is much appreciated but I was wondering when I might want to attach my context to the InheritedWidget vs not. Are there advantages to using inheritFromWidgetOfExactType vs ancestorInheritedElementForWidgetOfExactType under certain circumstances?

@robertohuertasm
Copy link
Owner

Hi Larry, I saw your message in the blog but I'm a little bit busier than usual these days. Anyway, I think it's better to ask here so everyone can participate and suggest changes or improvements.

Regarding your question, if you refer to the Flutter documentation about the InheredtedWidget you can see the following:

To obtain the nearest instance of a particular type of inherited widget from a build context, use BuildContext.inheritFromWidgetOfExactType.

Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state.

Pay special attention to the second paragraph.

Also, from the inheritFromWidgetOfExactType docs:

Obtains the nearest widget of the given type, which must be the type of a concrete InheritedWidget subclass, and registers this build context with that widget such that when that widget changes (or a new widget of that type is introduced, or the widget goes away), this build context is rebuilt so that it can obtain new values from that widget.

So basically, if you use inheritFromWidgetOfExactType (the default in this library) any change in the state of the BlocProvider, which is an InheritedWidget, will cause the build context used to retrieve it to be rebuilt.

If you use ancestorInheritedElementForWidgetOfExactType this won't happen. It's that simple.

So if you want your widgets to get rebuilt once the BlocProvider state changes (not that this has nothing to do with the streams that your Bloc may have), or a new instance is introduced or goes away, it's the way to go.

@larryq
Copy link
Author

larryq commented Apr 26, 2019

I see now, thanks for the clarification. The flutter docs, which are very good, were a bit confusing to me in this instance; for example, when saying:

this build context with that widget

I wasn't quite sure what this build context and that widget were referring to. Was 'that widget' the InheritedWidget or my own, for example? I suspected the former but really couldn't tell for sure. Appreciate the assist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants