-
Notifications
You must be signed in to change notification settings - Fork 34
How to make network calls? #31
Description
The title of this issue could be a little misleading but I was not sure how else to describe my thought process, so please stay with me and read along.
I am writing a Flutter application that uses flutter_flux. I have a few stores and a few views (widgets) so far. Right now I am in a dilemma on how to do the network API calls. My widget loads but does not have any data to display yet, which is fine, but now it needs to call my network API service that responds with data. Here is what I am thinking design-wise. Please share your opinions on it.
Widget -> Action -> Store -> Network Service Call -> trigger()
In this design, the widget kicks off an action something like fetchMessages and the store listening to this action MessageStore receives a call to this action. In the action callback, this store kicks the network call that returns Future. In the then() callback of that Future, this store mutates it's own data and calls trigger() so that the original view that started this now receives a callback. Similarly, also in catchError of the same Future, this store calls trigger() with some other mutation that maintains error states.
In this design, I am not exactly sure where do I call action from the widget? In build or in initState?
My thinking is that in initState the widget will grab data from store but it will be null or empty initially, and build will render empty view but at the same time, kickstarts the cycle described above, which calls back to the widget later and the build function now has data to render, re-grabbed from store.
Do you think it is a good pattern? It still abides to Flux's unidirectional data flow and does not let store data to be manipulated in any way other than an action callback.