How to remove old images from cache, Flutter

Issue

I user the flutter_cached_network_image for the cache.

I want to delete the old images from the cache.
If the image is not open or access or cache not accessed, for 2 weeks then simply remove it from the cache, to decrease the size of the app and remove the useless images from the cache which is no longer needed.

I am building a social media app.

Any solutions ????

Solution

You can use flutter_cache_manager to handle the cache life-cycle.

CachedNetworkImage also comes with cacheManager: parameter.

  CachedNetworkImage(
            imageUrl: "http://via.placeholder.com/350x150",
            cacheManager: CustomCacheManager.instance, //-< here
          ),

And the manager

class CustomCacheManager {
  static const key = 'customCacheKey';
  static CacheManager instance = CacheManager(
    Config(
      key,
      stalePeriod: const Duration(days: 14), // two weeks
      // others....
    ),
  );
}

Answered By – Yeasin Sheikh

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *