clearCache method
Clears the browser's Cache Storage.
Implementation
@override
Future<void> clearCache() async {
try {
// The Cache API is only available in secure contexts (HTTPS), except for localhost.
// We use a try-catch block to handle cases where it might be unavailable.
final JSArray<JSString> keys = await window.caches.keys().toDart;
final List<String> dartKeys =
keys.toDart.map((jsString) => jsString.toDart).toList();
// Filter for caches that are typically created by Flutter web apps.
final flutterCacheKeys =
dartKeys
.where(
(key) =>
key.startsWith('flutter-') || key.contains('-asset-cache'),
)
.toList();
if (flutterCacheKeys.isEmpty) {
debugPrint('WebCacheClear: No caches found to clear.');
return;
}
debugPrint(
'WebCacheClear: Found Flutter caches: $flutterCacheKeys. Attempting to delete...',
);
for (final String key in flutterCacheKeys) {
// The key needs to be converted back to JSString for the delete method.
await window.caches.delete(key).toDart;
debugPrint('WebCacheClear: Deleted cache with key: $key');
}
} catch (e) {
debugPrint(
'WebCacheClear: An error occurred while clearing cache: $e. This can happen if the site is not served over HTTPS.',
);
}
}