dioCache function
Downloads and caches a file with the specified extension.
url
: The file URL.ttl
: Cache duration.extFile
: File extension (e.g., jpg, png, pdf).folder
: Storage folder (default:"default"
).showLog
: Enables logging iftrue
.dio
: Optional Dio instance for testing.
Returns the downloaded File.
Example:
File file = await dioCache(
'https://example.com/image/123',
ttl: Duration(days: 1),
extFile: 'jpg',
folder: 'images',
showLog: true,
);
Implementation
Future<File> dioCache(
String url, {
required Duration ttl,
required String extFile, // extension filename (jpg or png or pdf / etc)
String? folder = "default",
bool showLog = false, // Default to false
Dio? dio, // Inject Dio for testing
}) async {
int ttlInSeconds = ttl.inSeconds; // Convert Duration to seconds
if (showLog && kDebugMode) {
Logger.log('Clearing expired cache for folder: $folder');
}
await clearExpiredCache(
showLog, ttlInSeconds, folder); // Clear expired cache dynamically
// Check if cached file exists and is valid
File? cachedFile =
await checkCachedFile(showLog, url, folder!, ttlInSeconds, extFile);
if (cachedFile != null) {
if (showLog) Logger.log('✅ Using cached file: ${cachedFile.path}');
return cachedFile;
}
// Download and store file
return await downloadFile(showLog, url, extFile, folder, ttlInSeconds,
dio: dio);
}