Best practice to report errors in Flutter

Viewed 1607

I'm trying to use firebase_crashlytics lib with my Flutter project, the problem is that Flutter never crashes, I can throw exceptions in the code, but all exceptions in dart are considered non-fatal exceptions. Those will be reported normally to the console, but I wanted Flutter, native, and unhandled exceptions to be considered fatal exceptions.

I wanted one of the following:

  • Crash the app when I have an unexpected exception (close the app with "not responding")
  • Report fatal exception to firebase console
  • A work around?

My main.dart code:

Future main() async {
  runZoned<Future<void>>(() async {
    await DotEnv().load('.env');
    setupNotifications();
    FlutterError.onError = Crashlytics.instance.recordFlutterError;
    runApp(MyApp());
  }, onError: Crashlytics.instance.recordError);
}

What I already tried to do:

  • SystemChannels.platform.invokeMethod('SystemNavigator.pop'); It works, but not really user friendly
  • Call kotlin function that tried to crash the app via MethodChannel, didn't work.

Things I could probably do:

  • Call a toast to notify the user that something went wrong
  • Show a pop-up notifying the user that something went wrong and close the app
  • Just close the app
  • Try to load the initial state of the application main screen
1 Answers

The behavior you are seeing is the default behavior for Crashlytics on Flutter, at least for now. If you want to exit your app I can recommend you the following options:

  1. Navigate to an error screen, showing a generic error message using Navigator.pushAndRemoveUntil(). then when the user clicks OK or after a timer, you exit the app. In order to exit the App you can throw and exception on the native side or use SystemChannels.platform.invokeMethod('SystemNavigator.pop'); although your App might be blocked at the App Store, as Apple rejects programatically exits.

  2. Be careful to have an error screen, with a friendly error message, ready for each possible error, so the user won't have to exit the app.

Related