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

Initializing stuff with store.dispatch #11

Closed
Pacane opened this issue Feb 19, 2018 · 2 comments
Closed

Initializing stuff with store.dispatch #11

Pacane opened this issue Feb 19, 2018 · 2 comments

Comments

@Pacane
Copy link

Pacane commented Feb 19, 2018

Hi,

I have this pattern where I initially injected a store instance in all my components using it (I wasn't aware of new StoreProvider.of(...)).

In my App, in some places, I also do 1 time initializations (ie: fetch data via store.dispatch(...)) in initState(). Previously, I could do it fine, because I had an instance of my Store.

Now I tried to move towards to new StoreProvider.of(...), but I faced the issue where context.inheritFromWidgetOfExactType(StoreProvider) can't be called in initState. Is there a way to achieve that somehow?

Seems like the Flutter docs suggest that I use didChangeDependencies() but that is not called only 1 time.

Thanks

@brianegan
Copy link
Owner

brianegan commented Feb 19, 2018

Hey hey :)

I can't speak to a custom implementation you've got going, but if you're using the "out of the box" StoreConnector and StoreBuilder Widgets, you can provide an onInit or onDispose function that will be fired when the Widget is first initialized and when it's disposed. Those would probably be my recommended way of initializing / disposing data at the right time.

To fix up your own implementation, the Trick I use: the StoreConnector widget itself doesn't access the Store using context within the initState method, but rather creates a new Widget in the build method where Context is available and passes the Store to that widget. Then the Store is available in the State class as the widget.store property.

https://github.com/brianegan/flutter_redux/blob/master/lib/flutter_redux.dart#L155

Do either of those options work for ya?

@Pacane
Copy link
Author

Pacane commented Feb 19, 2018

The first option did it, thanks!

I wasn't aware of the onInit function.

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