0

Is there any way to cache network image in mobile storage for long time in sharedPreference?

I am using cached_network_image package but it saves the images only when the app is online but I want to save it in shared Preference so that i can store it for long time

Muhammad Tameem Rafay
  • 3,602
  • 2
  • 21
  • 32
  • Possibly you could download your images to the client, creating your own "cache." You can use an API to tell your app when images need to be downloaded and replaced. Then you just load the images from assets instead of the network. https://stackoverflow.com/questions/58652546/flutter-how-to-download-asset-when-apps-launched-and-used-it – daddygames Aug 04 '21 at 12:32

1 Answers1

0

cached_network_image (or better said flutter_cache_manager) stores the images for long time - no need for shared preferences.

But it only does so, when instructed by the cache headers. So you have to make sure your server is returning the correct Cache-Control headers.

If this is not possible, you can create your own FileService and for the FileServiceResponse pass a validTill which is far in the future.

You can check the source code of the package for the default FileService implementation which looks at the Cache-Control headers:

https://github.com/Baseflow/flutter_cache_manager/blob/3bd49b866d9b5e6ce09d2283f0ef97488be97e1d/flutter_cache_manager/lib/src/web/file_service.dart#L85-L106

Herbert Poul
  • 4,512
  • 2
  • 31
  • 48