clearDiskCachedImages function

Future<bool> clearDiskCachedImages (
  1. {Duration duration}
)

clear the disk cache directory then return if it succeed.

timespan to compute whether file has expired or not

Implementation

Future<bool> clearDiskCachedImages({Duration duration}) async {
  try {
    final Directory cacheImagesDirectory = Directory(
        join((await getTemporaryDirectory()).path, cacheImageFolderName));
    if (cacheImagesDirectory.existsSync()) {
      if (duration == null) {
        cacheImagesDirectory.deleteSync(recursive: true);
      } else {
        final DateTime now = DateTime.now();
        for (final FileSystemEntity file in cacheImagesDirectory.listSync()) {
          final FileStat fs = file.statSync();
          if (now.subtract(duration).isAfter(fs.changed)) {
            //print("remove expired cached image");
            file.deleteSync(recursive: true);
          }
        }
      }
    }
  } catch (_) {
    return false;
  }
  return true;
}