如何 enable/disable 在 Flutter 中使用 Firebase Crashlytics
How to enable/disable Firebase Crashlytics in Flutter
我正在尝试在我的 Flutter 应用程序中实施一种选择加入机制,其中用户可以选择 enable/disable 将 Crashlytics 数据从他们的设备发送到我的 Firebase 控制台。
阅读这篇文章 https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#enable-reporting,我发现您最初可以通过 AndroidManifest 文件禁用它,并且可以在运行时通过以下代码禁用它:
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)
但是,我很难找到类似的 dart 代码来访问类似的功能。
注意:顺便说一下,我使用的是 Flutter 的 firebase_crashlytics 插件。
首先,您需要一个全局变量之类的东西,例如 enableCrashlytics
来指示是否应将此崩溃发送给 Firebase。然后像这样在 FlutterError.onError
中使用该变量:
FlutterError.onError = (details){
if(enableCrashlytics) {
enableCrashlytic = false;
Crashlytics.instance.recordFlutterError(details);
} else{
//Do nothing
}
};
调用 setCrashlyticsCollectionEnabled
方法来切换 Crashlytics 收集状态。
例如,要确保当您的应用程序处于调试模式时它被禁用,您可以执行以下操作:
if (kDebugMode) {
// Force disable Crashlytics collection while doing every day development.
// Temporarily toggle this to true if you want to test crash reporting in your app.
await FirebaseCrashlytics.instance
.setCrashlyticsCollectionEnabled(false);
} else {
// Handle Crashlytics enabled status when not in Debug,
// e.g. allow your users to opt-in to crash reporting.
}
完整示例
// Toggle this for testing Crashlytics in your app locally.
const _kTestingCrashlytics = true;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runZonedGuarded(() {
runApp(MyApp());
}, FirebaseCrashlytics.instance.recordError);
}
class _MyAppState extends State<MyApp> {
Future<void> _initializeFlutterFireFuture;
// Define an async function to initialize FlutterFire
Future<void> _initializeFlutterFire() async {
// Wait for Firebase to initialize
if (_kTestingCrashlytics) {
// Force enable crashlytics collection enabled if we're testing it.
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
} else {
// Else only enable it in non-debug builds.
// You could additionally extend this to allow users to opt-in.
await FirebaseCrashlytics.instance
.setCrashlyticsCollectionEnabled(!kDebugMode);
}
// Pass all uncaught errors to Crashlytics.
Function originalOnError = FlutterError.onError;
FlutterError.onError = (FlutterErrorDetails errorDetails) async {
await FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
// Forward to original handler.
originalOnError(errorDetails);
};
}
@override
void initState() {
super.initState();
_initializeFlutterFireFuture = _initializeFlutterFire();
}
}
我正在尝试在我的 Flutter 应用程序中实施一种选择加入机制,其中用户可以选择 enable/disable 将 Crashlytics 数据从他们的设备发送到我的 Firebase 控制台。
阅读这篇文章 https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#enable-reporting,我发现您最初可以通过 AndroidManifest 文件禁用它,并且可以在运行时通过以下代码禁用它:
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)
但是,我很难找到类似的 dart 代码来访问类似的功能。
注意:顺便说一下,我使用的是 Flutter 的 firebase_crashlytics 插件。
首先,您需要一个全局变量之类的东西,例如 enableCrashlytics
来指示是否应将此崩溃发送给 Firebase。然后像这样在 FlutterError.onError
中使用该变量:
FlutterError.onError = (details){
if(enableCrashlytics) {
enableCrashlytic = false;
Crashlytics.instance.recordFlutterError(details);
} else{
//Do nothing
}
};
调用 setCrashlyticsCollectionEnabled
方法来切换 Crashlytics 收集状态。
例如,要确保当您的应用程序处于调试模式时它被禁用,您可以执行以下操作:
if (kDebugMode) {
// Force disable Crashlytics collection while doing every day development.
// Temporarily toggle this to true if you want to test crash reporting in your app.
await FirebaseCrashlytics.instance
.setCrashlyticsCollectionEnabled(false);
} else {
// Handle Crashlytics enabled status when not in Debug,
// e.g. allow your users to opt-in to crash reporting.
}
完整示例
// Toggle this for testing Crashlytics in your app locally.
const _kTestingCrashlytics = true;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runZonedGuarded(() {
runApp(MyApp());
}, FirebaseCrashlytics.instance.recordError);
}
class _MyAppState extends State<MyApp> {
Future<void> _initializeFlutterFireFuture;
// Define an async function to initialize FlutterFire
Future<void> _initializeFlutterFire() async {
// Wait for Firebase to initialize
if (_kTestingCrashlytics) {
// Force enable crashlytics collection enabled if we're testing it.
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
} else {
// Else only enable it in non-debug builds.
// You could additionally extend this to allow users to opt-in.
await FirebaseCrashlytics.instance
.setCrashlyticsCollectionEnabled(!kDebugMode);
}
// Pass all uncaught errors to Crashlytics.
Function originalOnError = FlutterError.onError;
FlutterError.onError = (FlutterErrorDetails errorDetails) async {
await FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
// Forward to original handler.
originalOnError(errorDetails);
};
}
@override
void initState() {
super.initState();
_initializeFlutterFireFuture = _initializeFlutterFire();
}
}