init static method
init function initializes the cache management system. Use this code only once in the app in main to avoid errors.
You can provide a subDir
where the boxes should be stored.
clearCacheAfter
property is used to set a duration after which the cache will be cleared.
Default value of clearCacheAfter
is 7 days which means if clearCacheAfter
is set to null,
an image cached today will be cleared when you open the app after 7 days from now.
Implementation
static Future<void> init({String? subDir, Duration? clearCacheAfter}) async {
if (_isInitialized) return;
clearCacheAfter ??= const Duration(days: 7);
await Hive.initFlutter(subDir);
_isInitialized = true;
_imageKeyBox = await Hive.openLazyBox(_BoxNames.imagesKeyBox);
_imageBox = await Hive.openLazyBox(_BoxNames.imagesBox);
await _clearOldCache(clearCacheAfter);
}