getCachedData<T> static method
Retrieve cached data
Implementation
static T? getCachedData<T>(String key) {
_ensureInitialized();
final cachedString = _prefs!.getString(key);
if (cachedString == null) return null;
try {
final cacheItem = jsonDecode(cachedString);
final timestamp = cacheItem['timestamp'] as int;
final ttl = cacheItem['ttl'] as int?;
// Check if cache has expired
if (ttl != null) {
final now = DateTime.now().millisecondsSinceEpoch;
if (now - timestamp > ttl) {
// Cache expired, remove it
removeCachedData(key);
return null;
}
}
return cacheItem['data'] as T?;
} catch (e) {
// If there's an error parsing, remove the corrupted cache
removeCachedData(key);
return null;
}
}