flutter 崩溃收集的几种方式
一、flutter 崩溃收集的方式
1、通用方式
use a try/catch block
2、捕捉async异常
1)try/catch
Future main() async {
var dir = new Directory('/tmp');
try {
var dirList = dir.list();
await for (FileSystemEntity f in dirList) {
if (f is File) {
print('Found file ${f.path}');
} else if (f is Directory) {
print('Found dir ${f.path}');
}
}
} catch (e) {
print(e.toString());
}
}
2)使用 Future API
myFunc()
.then((value) {
doSomethingWith(value);
...
throw("some arbitrary error");
})
.catchError(handleError);
3)async异常 与 Future 的更多信息
Futures and Error Handling
Asynchronous Programming: Futures
dart:async library
3、使用runZoned
4、使用 FlutterError.onError
5、使用 Isolate.current.addErrorListener
use Isolate.current.addErrorListener to capture uncaught errors in the root zone.
Isolate.current.addErrorListener(new RawReceivePort((dynamic pair) async {
print('Isolate.current.addErrorListener caught an error');
await _reportError(
(pair as List<String>).first,
(pair as List<String>).last,
);
}).sendPort);
二、Flutter异常收集最佳实践
Future<Null> main() async {
FlutterError.onError = (FlutterErrorDetails details) async {
if (isInDebugMode) {
FlutterError.dumpErrorToConsole(details);
} else {
Zone.current.handleUncaughtError(details.exception, details.stack);
}
};
runZoned<Future<Null>>(() async {
runApp(new HomeApp());
}, onError: (error, stackTrace) async {
await _reportError(error, stackTrace);
});
}
1、Sentry
1)商业Sentry服务器
https://sentry.io
sentry.io 的服务是收费的,使用天数只有13天左右,过期后不付费的话:只保存1w个事件,没有成员功能。
2)flutter官方支持
Sentry flutter package:https://pub.dartlang.org/packages/sentry
3)总结
优点:有点因为有了 flutter package ,flutter 接入 Sentry,非常简单。
缺点:免费版限制多。
2、Crashlytics (--> fabric --> Firebase)
1)flutter 官方支持的计划
Please add support for Crashlytics
2)非官方flutter插件
https://github.com/kiwi-bop/flutter_crashlytics
https://pub.dartlang.org/packages/flutter_crashlytics
3)总结
Firebase
优点:Google旗下,免费!