fluttercachingperiodic-taskflutter-sharedpreference

How to store response of an api in cache and call api after every 12 hours?


I have an api call that have list of images and text in json response, i want to store json in local and use it in the ui, The api should be called after every 12 hours and save data in cache or the internal storage. Api call is in the listscreen that opens on a button click.

which one is more suitable. How can i call an api in certain screen even when the app is closed? Which one is more prefereble to store response cache or using sharedprefrence? I have used dio and retrofit as sub for the api call.

I tried using Cron and save data in sharedpref. i tried using flutter cache manager but didnt fit well. i tried using dio cache mnanager but it didnt work well with retrofit submodule with dio.

Please suggest solution for this.


Solution

  • I have done by using shared preference and timer object, As i stored my response in preference and put a timer in the background, As per requirement the api should be called after each 12 hours finished.

    So main logic was i start the timer when the app is launched and i store the time stamp when user closes the app, so when user again start the app i check with the last time user has closed the app and counts the time if its less then 12 hours I will continue timer or I will call the API.

    So this is how I managed to save API response in local and if the data is not found in the preference I would call the API right away.