一、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 的更多信息
- <u style="text-decoration: none; border-bottom: 1px dashed grey;">Futures and Error Handling</u>
- <u style="text-decoration: none; border-bottom: 1px dashed grey;">Asynchronous Programming: Futures</u>
- <u style="text-decoration: none; border-bottom: 1px dashed grey;">dart:async library</u>
3、使用 runZoned
// This creates a [Zone] that contains the Flutter application and stablishes
// an error handler that captures errors and reports them.
//
// Using a zone makes sure that as many errors as possible are captured,
// including those thrown from [Timer]s, microtasks, I/O, and those forwarded
// from the `FlutterError` handler.
//
// More about zones:
//
// - https://api.dartlang.org/stable/1.24.2/dart-async/Zone-class.html
// - Zones
runZoned<Future<Null>>(() async {
runApp(new CrashyApp());
}, onError-: (error, stackTrace) async {
await _reportError(error, stackTrace);
});
4、使用 FlutterError.onError
FlutterError.onError is only about reporting flutter framework errors (errors that are caught by the framework typically during a frame).
// This captures errors reported by the Flutter framework.
FlutterError.onError = (FlutterErrorDetails details) async {
if (isInDebugMode) {
// In development mode simply print to console.
FlutterError.dumpErrorToConsole(details);
} else {
// In production mode report to the application zone
// 重定向到 3中的 runZone 中處理
Zone.current.handleUncaughtError(details.exception, details.stack);
}
};
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異常收集最佳實(shí)踐
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);
});
}
三、Flutter crash 收集平臺(tái)
1、Sentry
1)商業(yè)Sentry服務(wù)器
Sentry | Error Tracking Software — JavaScript, Python, PHP, Ruby, more
Sentry官方的服務(wù)是收費(fèi)的,使用天數(shù)只有13天左右,過(guò)期后不付費(fèi)的話:只保存1w個(gè)事件,沒(méi)有成員功能。
2)flutter官方支持
Sentry flutter package:<u style="text-decoration: none; border-bottom: 1px dashed grey;">https://pub.dartlang.org/packages/sentry</u>
demo: flutter/crashy 、 <u style="text-decoration: none; border-bottom: 1px dashed grey;">yjbanov</u>/crashy
3)總結(jié)
優(yōu)點(diǎn):有了 flutter package ,flutter 接入 Sentry,非常簡(jiǎn)單。
缺點(diǎn):免費(fèi)版限制多。
2、Crashlytics (--> fabric --> Firebase)
1)flutter 官方支持的計(jì)劃
<u style="text-decoration: none; border-bottom: 1px dashed grey;">Please add support for Crashlytics</u>
2)非官方flutter插件
<u style="text-decoration: none; border-bottom: 1px dashed grey;">https://github.com/kiwi-bop/flutter_crashlytics</u>
https://pub.dartlang.org/packages/flutter_crashlytics
3)總結(jié)
優(yōu)點(diǎn):Google旗下,免費(fèi)!
缺點(diǎn):目前flutter官方尚未有對(duì)應(yīng)package/plugin,使用 <u style="text-decoration: none; border-bottom: 1px dashed grey;">flutter_crashlytics</u> 對(duì)接比較麻煩。
注意:2019年中,fabric 服務(wù)將關(guān)閉,一律遷移至 Firebase。這個(gè)動(dòng)作與微軟的 hockeyapp 很像。
備注轉(zhuǎn)自:https://zhuanlan.zhihu.com/p/54142949