This is how you programmatically trigger a fatal crash in Flutter, maybe for testing crash reporting (like Sentry, Firebase Crashlytics, etc.), etc.
Here’s a simple way to cause a fatal crash in Flutter:
void triggerFatalCrash() {
Future.delayed(Duration.zero, () {
throw StateError('This is a fatal crash triggered manually.');
});
}
How this works:
Future.delayed(Duration.zero, ...)puts the exception into the event loop and crashes the app outside of the normal try-catch zone, causing an unhandled exception.StateError(or any exception) that is uncaught will kill the app if you’re not catching it globally.
Alternatively, if you want to simulate a native crash (more hardcore crash, like segmentation fault-style), you can use platform channels to trigger native code (for example, causing a crash in Android or iOS).
For example, if using Firebase Crashlytics SDK, it provides a method for forced crash:
FirebaseCrashlytics.instance.crash();
Important notes:
- In debug mode, Flutter sometimes just logs errors instead of crashing. Run in release mode or profile mode to see true fatal behavior.
- Always be careful using manual crashes — they can crash your app entirely during testing if not isolated.
