How to enable/disable Firebase Crashlytics in Flutter

3,520

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
    }
  };
Share:
3,520
AverageCoder
Author by

AverageCoder

Updated on November 28, 2022

Comments

  • AverageCoder
    AverageCoder over 1 year

    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.

  • AverageCoder
    AverageCoder about 4 years
    Thank you!!! This is some great insight. Just to confirm, I can just enable Crashlytics by default in the AndroidManifest file and any Flutter error that is supposed to be sent to the console should pass through this condition telling if it can be sent or not right? Also, is this different from the onError parameter for runZoned?
  • The Vinh Luong
    The Vinh Luong about 4 years
    Actually I don't think you need to follow instructions here at all. Just following the integration instruction from firebase_crashlytic plugin and it should be auto logging crash just fine. About onError in runZoned: You should also catch error in runZoned, this is where all the error caused by asynchronously executed code being caught. And you can enable/disable Firebase reporting just the same as the code snippet I posted.
  • AverageCoder
    AverageCoder about 4 years
    I patterned the syntax you gave for runZoned with some little modifications. It seems to go well! Thanks again, I was actually in a slump with my project because of this. You're a lifesaver :)