Skip to content

Question: dispatching action on widget initialization #34

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
maxlapides opened this issue Sep 24, 2018 · 4 comments
Open

Question: dispatching action on widget initialization #34

maxlapides opened this issue Sep 24, 2018 · 4 comments

Comments

@maxlapides
Copy link

I'd like to dispatch an action when a widget initializes. Normally, I'd put this kind of logic inside initState on a stateful Flutter widget. But, AFAIK, no such feature exists for widgets that extend StoreConnector.

The reason I'd like to do this is so I can do things like make API calls when a widget is initialized. The parallel in the React world would be calling fetch inside of componentDidMount. I think an argument could be made that a better architecture in many cases would be to put the API calls in some middleware that is triggered by a change route action, and this is probably what I'll end up doing. But, I was really interested to hear what the best practice here is. Thanks!

@tdesc
Copy link

tdesc commented Oct 15, 2018

Please give us initState for living!

@maxlapides
Copy link
Author

The more I think about it, the more I think initState would be an excellent feature for this library, but I'm not sure what the right way to implement it is. @davidmarne if you can point me in the right direction, I would be happy to work on a PR if you agree?

@Will-W
Copy link

Will-W commented Dec 11, 2018

My solution to this has been to create a child stateful widget in between my StoreConnection and the widget that I would normally pass straight to builder:.

I can then override initState on that widget which gives me a place to dispatch my action. It's a bit tediously boilerplate, but it works.

@jamesncl
Copy link

jamesncl commented Jun 4, 2020

@smiLLe has a great PR (38) which solves these issues by including these really useful callbacks onInit, onFirstBuild, onDidChange, onDispose which are present in the original flutter_redux package. For me these are pretty much essential, and not sure why they're not merged into main yet.

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

No branches or pull requests

4 participants