Group Group Group Group Group Group Group Group Group

State Management With Provider |

See how to architect your Flutter app using Provider, letting you readily handle app state to update your UI when the app state changes.

This is a companion discussion topic for the original entry at

Correct me if I’m wrong I have a question. Since the API results are being cache. What can I do to trigger a refresh to get the latest rates?

You could add a button or RefreshIndicator to initiate a manual refresh. Then perhaps add a forceRefresh flag to the fetchExchangeRates() method and handle it there.

1 Like

After this article was published I realized I should have used the ChangeNotifierProvider.value constructor in the last code block.

So change this:

return ChangeNotifierProvider<ChooseFavoritesViewModel>(
      create: (context) => viewModel,

To this:

return ChangeNotifierProvider<ChooseFavoritesViewModel>.value(
      value: viewModel,

The reason is that you are not creating a new view model, but just providing the one you had already created previously in initState().

TODO: Fix this in the next tutorial update.

Hi suragch. Dont know if it’s me, but i’ve tried to run the start and the final projects and both crashed at the start.

Is it ok to change my viewmodel that on serviceLocator from registerFactory to registerLazySingleton because I want to send events from the services not just from view

Did you ever get it to work? I’ve been meaning to recheck this tutorial, but I’ve been a little swamped with work recently.

Using registerFactory creates a new instance of your view model every time you call it. This is useful if you want to use the same view model code in two different parts of your app. I ran into this recently when I made an app that had two audio players that shared a single view model class. If I had used a singleton for the view model then both audio player widgets would have been modifying the same instance of the view model and neither one would have worked. Since I used separate instances of the view model, they could each manage their own audio widget.

If you only need one instance of a view model, could you use registerLazySingleton? Maybe. I’m not really sure. You could try it, but I’d keep an eye out for bugs. I don’t understand your use case so I can’t give a great answer. If you are making a service, then registerLazySingleton is how I create services, too. But view models are different than services. They are they controller between the services and the UI.

1 Like

@suragch Thank you for sharing this - much appreciated!

First, thank you for your response.
In my situation, I have a view model that depends on a realtime database (Socket IO), so when my database that on the server change, it sends emit to the client-side in order to rebuild the widgets, and I put the SocketIO as a service.

Although I haven’t done any socket IO work myself, if I were approaching this problem, I would make a service to handle the socket IO communication with the server. I’d have that service expose the data it receives from the server as a stream or maybe a callback. Then I’d have the view model listen to the stream from the service. The view model would in turn put the data in a form that could be directly shown in the UI. The view model could also take events from the UI and pass them on to the service by calling some method on the service. In this way the service and the UI never directly interact. Everything is interpreted by the view model. So back to your original question, there is no reason to make your view model a singleton in this case.

Thanks, I will try to make a stream on Socket IO service in order to send events to the view model.