isInCrashLoop method
Checks if the app is in a crash loop (3+ crashes within 60 seconds).
Call once at app startup. If this returns true, show a minimal recovery view that doesn't depend on any Flutter widgets.
Implementation
Future<bool> isInCrashLoop() async {
try {
final prefs = await SharedPreferences.getInstance();
final count = prefs.getInt(_crashCountKey) ?? 0;
final lastMs = prefs.getInt(_lastLaunchKey) ?? 0;
final now = DateTime.now().millisecondsSinceEpoch;
final elapsed = now - lastMs;
if (elapsed > 60000) {
// More than 60 seconds since last launch — reset counter
await prefs.setInt(_crashCountKey, 1);
await prefs.setInt(_lastLaunchKey, now);
return false;
} else {
final newCount = count + 1;
await prefs.setInt(_crashCountKey, newCount);
await prefs.setInt(_lastLaunchKey, now);
return newCount >= 3;
}
} catch (_) {
return false;
}
}