Flutter. App won't crash and send crash report

Viewed 1591

I've integrated Firebase Crashlytics in my app and I'm testing the Android app. I've forced a crash to check it:

if (true){
  List arr = [];
  throw arr[1] =2;
}

My problem is that the app doesn't crash. I just get this in the logs but it won't crash in order for Crashlytics to send the stacktrace to the server.

E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 1

enter image description here

Why is this happening and why is this not "working properly".

I know one would not want to have their app crash, but ... how will I know if the app crashes while in production?

P.S. Testing with FirebaseCrashlytics.instance.crash() works on the other hand. App crashes and sends the report. Why does that work?!

1 Answers

I was faced with the same issue and found answers to your questions and solution that works.

Why is this happening and why is this not "working properly"

According to Crashlytics there are two types of events: Crashes and Non-fatals. Unhandled Exceptions like yours treated as Non-fatal events and Crashlytics doesn't record them by default. https://firebase.flutter.dev/docs/crashlytics/reports#filtering-crash-types

but ... how will I know if the app crashes while in production?

You can enable recording of Non-fatals by doing this in main.dart

FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;

https://firebase.flutter.dev/docs/crashlytics/usage#handling-uncaught-errors

P.S. Testing with FirebaseCrashlytics.instance.crash() works on the other hand. App crashes and sends the report. Why does that work?!

Because FirebaseCrashlytics.instance.crash() causes the app to crash natively. https://pub.dev/documentation/firebase_crashlytics/latest/firebase_crashlytics/FirebaseCrashlytics/crash.html


From comments:

I think it's more frustrating for the user if he clicks and nothing happens as opposed to the app crashing. If it crashes, it's clear then what has happened, there's an issue, as opposed to nothing happening and leaving the user flabbergasted...

Additionally to submitting a report of a non-fatal errors you can implement one of the following scenarios:

  1. Terminate an app (simulate a crash)

exit(0) terminates an app and doesn't report additional crash to Crashlytics

FlutterError.onError = (FlutterErrorDetails details) async {
    await FirebaseCrashlytics.instance.recordFlutterError(details);
    exit(0);
};
  1. Display some alert like "Something went wrong"

Probably display an alert is better depending on data your application processing and nature of operations.

Related