[英]How to handle asynchronous error in 'Add to App' Flutter android application
I am trying to use runZonedGuarded for the handling of asynchronous error in my Add to App project, but its never goes under the error handling when debugging, only showing as an unhandled exception in logs.我正在尝试使用 runZonedGuarded 来处理我的Add to App项目中的异步错误,但它在调试时从不进行错误处理,仅在日志中显示为未处理的异常。
Future<void> main() async {
unawaited(runZonedGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (FlutterErrorDetails errorDetails) {
print('This is an error on the Flutter SDK');
print(errorDetails.exception);
print('-----');
print(errorDetails.stack);
};
runApp(app);
}, (error, stackTrace) {
print('Exception handled');
print(error);
print('-----');
print(stackTrace);
}));
Creating the asynchronous exception on button tap as below -如下所示在点击按钮时创建异步异常 -
class DebugPage extends StatelessWidget {
DebugPage({
Key key,
this.navigatorKey,
}) : super(key: key);
void createError() async {
print('Creating exception.');
Future.delayed(Duration.zero, () => throw Exception('async error'));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Debug"),
),
body: ListView(
children: <Widget>[
ListTile(title: Text('Test Network Kit....'),
onTap: () {
createError();
}),
],
),
);
}
}
I tried the same process for full flutter app and its working in that project by catching the asynchronous error correctly.通过正确捕获异步错误,我对完整的 flutter 应用程序及其在该项目中的工作尝试了相同的过程。
Instead of printing error: print(stackTrace);
而不是打印错误:
print(stackTrace);
print it: print(DiagnosticsStackTrace('Stack', stackTrace).getProperties());
打印出来:
print(DiagnosticsStackTrace('Stack', stackTrace).getProperties());
So that you'll not be disturbed by flutter, and can handle all error properties.这样你就不会被 flutter 打扰,并且可以处理所有的错误属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.