

I've added async Counter._updatePreferences which is called in Counter.increment and crement, which saves the current value of the Counter to the shared_preferences.Ĭonst MyApp(. To reset the state when the user leaves a screen and re-enters it.As far as I know, Provider is not recommended to use with a stateful widget. I want to animate my widget but it seems that AnimateController needs a sync parameter that comes from the state of a stateful widget. I access the Counter via provider library in _MyHomePageState.build with context.watch(), which causes this to rebuild on changes (without requiring calls to setState. I use Provider to manage state in my Flutter demo.I added an async "constructor" to the Counter class that initializes from the shared_preferences.Executing something when the animation ends works as once it ends. I want to execute something (say reset the animation controller) once the animation ends OR a button (from another widget) is pressed. I'm trying to listen to a variable change to execute some code. In that case, probably FutureBuilder would be the correct solution. Listening to a variable change in flutter. (I don't think the loading page or delay are necessary if you aren't using the widget as your top level widget, which would probably be better anyway.

You must include the first runApp however, otherwise shared_preferences will not be able to correctly access the file containing these preferences on the device.Ī similar thing can be done with with FutureBuilder, but you must await a delay prior to attempting to read from shared_preferences. If it's the top level app, you can initialize the counter before actually using it, displaying a loading page during the load time (imperceptible in this case). I'm also new to flutter - the below may not be the best way, but it does work, so maybe it will help someone.
