loadStatsCache method
Load the stats cache from disk. Returns an empty cache if the file doesn't exist or is invalid.
Implementation
Future<PersistedStatsCache> loadStatsCache() async {
final cachePath = getStatsCachePath();
try {
final file = File(cachePath);
if (!await file.exists()) {
return _getEmptyCache();
}
final content = await file.readAsString();
final parsed = jsonDecode(content) as Map<String, dynamic>;
// Validate version.
final version = (parsed['version'] as num?)?.toInt() ?? 0;
if (version != statsCacheVersion) {
final migrated = _migrateStatsCache(parsed);
if (migrated == null) {
return _getEmptyCache();
}
await saveStatsCache(migrated);
if (shotStatsEnabled.value && migrated.shotDistribution == null) {
return _getEmptyCache();
}
return migrated;
}
final cache = PersistedStatsCache.fromJson(parsed);
// Basic validation.
if (cache.totalSessions < 0) {
return _getEmptyCache();
}
if (shotStatsEnabled.value && cache.shotDistribution == null) {
return _getEmptyCache();
}
return cache;
} catch (_) {
return _getEmptyCache();
}
}