flutter页面异常监测
在Flutter中,通常用FlutterError监测Flutter框架抛出的异常,用runZonedGuarded监测应用中用户代码异常。
class AppGuarded {
run(Widget app) {
//1. 用FlutterError监测flutter框架抛出的异常
FlutterError.onError = (FlutterErrorDetails details) async {
//线上环境,将异常交给zone统一处理
if (kReleaseMode) {
Zone.current.handleUncaughtError(details.exception, details.stack!);
} else {
//开发期间,走Console抛出
FlutterError.dumpErrorToConsole(details);
}
};
//2. runZonedGuarded监测整个用应用代码异常 不使用FlutterError默认只能捕捉业务代码异常
runZonedGuarded(() {
runApp(app);
}, (e, s) => _reportError(e, s));
}
///通过接口上报异常
_reportError(Object error, StackTrace s) {
print(‘kReleaseMode: k R e l e a s e M o d e ′ ) ; p r i n t ( ′ c a t c h e r r o r : kReleaseMode'); print('catch error: kReleaseMode′);print(′catcherror:error’);
}
}
————————————————
原文链接:https://blog.csdn.net/holyli1134516796/article/details/136409991