Call function once in view, upon initialization complete?
I'm using a FutureViewModel. It appears that ViewModelBuilder's onModelReady fires before futureToRun() is called. How does one call a function in the view once, right after the model is initialized?
I'm in this situation because I thought that the cleanest way to use Stacked would be to have the model not know about widgets at all, but rather just contain the state and logic that the view's widgets use. If I create widgets inside of futureToRun(), I think things will work, but is that the right pattern?
@willhaslett I do that all the time in onModelReady. That's why it's there. It's fired as soon as the model is created. You can set your callOnmodelReadyOnce property to true and then it'll only call once. Make sure to set dispose to false and make your viewmodel a singleton as well.
ok, thank you for your reply. To confirm, onModelReady fires after initialization, and the data structures that are initialized in my viewmodel should be available at that time? If so, I must be doing something else wrong and have a path forward.
@willhaslett onModelReady fires when the model is ready, not after the future has run or the streams have been subscribed too. What is it that you want to do? Maybe I can tell you how I implement that.
@FilledStacks I'm trying to do just what's in the title of the ticket, which essentially boils down to having an onInitializationComplete hook. The thought is that as soon as the viewmodel's data structures are built, I have a bunch of widgets to build that will only be built once, and I'd like to do that in the view, rather than having the viewmodel build widgets.
I'm guessing that I'm simply missing something about the right approach to this. The one given (I think) is that I need a FutureViewModel, as I have a bunch of async stuff to do during initialization.
Since this looks more like user support than an actual issue with the package, go ahead and close if you like. I'll figure it out :)
@willhaslett The future viewmodel is for a viewmodel that depends on data to be returned from a future. It will run the future and then store the result in data.
The way it usually works is like this.
- View shows then future runs
- Show a busy indicator in the app based on the model.isBusy property
- Show the model.data if model.isBusy is false
that's basically all there is to it. We don't specifically "build widgets with data". the data is passed with the contruction of a widget but you don't have to wait for anything. That's the most I can answer based on your messages. I hope it helps. Otherwise if you're struggling with stacked you might have some problems with understanding Flutter itself? If that's the case I'd say you should not be using an MVU architecture right away and instead get to know pure provider only, which I have a tutorial about.
The tutorial actually goes through how staccked is built and should be used. The video is here. If you do have experience in Flutter and strong programming experience then excuse my suggestion, I'm just trying to help out because the problem you're having is literally the only thing I show in my videos when we fetch posts, etc. unless I'm not understanding it correctly.
What do u do if u want to return more than one future?