B站视频
https://www.bilibili.com/video/BV1Ek4y1r7zk
本节目标
- 使用 sentry 平台
- flutter 集成
- android 集成
- ios 集成
正文
错误收集策略
sentry 平台
https://sentry.io
收集 flutter
- 参考
https://docs.sentry.io/platforms/flutter/
- pubspec.yaml
dependencies:
sentry: ^3.0.1
- lib/main.dart
// 创建 SentryClient 用于将异常日志上报给 sentry 平台
final SentryClient _sentry = new SentryClient(
dsn:
'https://xxxxxxxxxx',
);
// 是否开发环境
bool get isInDebugMode {
return false; // false 开始上传 sentry
}
// 上报异常的函数
Future<void> _reportError(dynamic error, dynamic stackTrace) async {
print('Caught error: $error');
if (isInDebugMode) {
print(stackTrace);
} else {
final SentryResponse response = await _sentry.captureException(
exception: error,
stackTrace: stackTrace,
);
if (response.isSuccessful) {
print('Success! Event ID: ${response.eventId}');
} else {
print('Failed to report to Sentry.io: ${response.error}');
}
}
}
Future<Null> main() async {
// 捕获并上报 Flutter 异常
FlutterError.onError = (FlutterErrorDetails details) async {
if (isInDebugMode == true) {
FlutterError.dumpErrorToConsole(details);
} else {
Zone.current.handleUncaughtError(details.exception, details.stack);
}
};
// 捕获并上报 Dart 异常
runZonedGuarded(() async {
await Global.init();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<AppState>.value(
value: Global.appState,
),
],
child: Consumer<AppState>(builder: (context, appState, _) {
if (appState.isG