Search code examples
firebasefluttercrashlyticsfirebase-crash-reporting

How to enable/disable Firebase Crashlytics in Flutter


I'm trying to implement an opt-in mechanism in my Flutter app wherein the user can choose to enable/disable sending Crashlytics data from their device to my Firebase console.

Reading from this article https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#enable-reporting, I found out that you can disable it initially via the AndroidManifest file and can be disabled on runtime through the code below:

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)

However, I am having a hard time finding the comparable dart code to access the similar functionality.

Note: I am using the firebase_crashlytics plugin for Flutter by the way.


Solution

  • Firstly, you need something like a global variable, for example enableCrashlytics to indicate whether this crash should be sent to Firebase or not. And then using that variable inside FlutterError.onError like this:

    FlutterError.onError = (details){
        if(enableCrashlytics) {
          enableCrashlytic = false;
          Crashlytics.instance.recordFlutterError(details);
        } else{
          //Do nothing
        }
      };