How to enable/disable Firebase Crashlytics in Flutter
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
}
};
![AverageCoder](https://lh3.googleusercontent.com/-qyYfxByPX8s/AAAAAAAAAAI/AAAAAAAAAAA/AAnnY7qDbyrEkJ7z_S0d-uuFA8-Fd7IQ2g/mo/photo.jpg?sz=256)
AverageCoder
Updated on November 28, 2022Comments
-
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 about 4 yearsThank 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 about 4 yearsActually 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. AboutonError
inrunZoned
: You should also catch error inrunZoned
, 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 about 4 yearsI 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 :)