run static method
Implementation
static run(Widget app) {
///主要用于捕获 Flutter 框架内部抛出的未处理异常,比如在 widget 树构建过程中或事件处理期间出现的异常。
FlutterError.onError = (FlutterErrorDetails details) async {
///线上环境
if (kReleaseMode) {
//开发期间 print
FlutterError.dumpErrorToConsole(details);
} else {
LackOff.addLog(LackOffBean(
logType: LogType.flutterRuntime,
logTitle: details.exception.toString(),
logDetail: details.stack.toString(),
date: DateTime.now().toString(),
));
}
};
///runZonedGuarded 用于捕获 Dart 异步任务中的异常,不仅限于 Flutter 框架,还包括自定义的 Dart 异步操作。例如网络请求、数据库操作、定时器等异步代码中的异常都可以被捕获。
runZonedGuarded(() {
runApp(app);
}, (Object error, StackTrace stack) {
debugPrint('AppCatchError message:$error,stack$stack');
LackOff.addLog(LackOffBean(
logType: LogType.dartRuntime,
logTitle: error.toString(),
logDetail: stack.toString(),
date: DateTime.now().toString(),
));
});
/// 添加全局 Isolate 异常捕获
Isolate.current.addErrorListener(RawReceivePort((dynamic pair) {
final List<dynamic> errorAndStacktrace = pair;
LackOff.addLog(LackOffBean(
logType: LogType.dartRuntime,
logTitle: errorAndStacktrace.first.toString(),
logDetail: errorAndStacktrace.last.toString(),
date: DateTime.now().toString(),
));
}).sendPort);
/// 捕获无法被 runZonedGuarded 捕获的异常 (例如 Timer 异常)
PlatformDispatcher.instance.onError = (error, stackTrace) {
LackOff.addLog(LackOffBean(
logType: LogType.dartRuntime,
logTitle: error.toString(),
logDetail: stackTrace.toString(),
date: DateTime.now().toString(),
));
return true;
};
}