如何处理"添加到应用程序"中的异步错误 颤振安卓应用程序



我试图使用runzonedprotected在我的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);
}));

按下按钮创建异步异常-

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();
}),
],
),
);
}
}

我尝试了相同的过程全扑动应用程序和它的工作在该项目中捕获异步错误正确。

代替打印错误:print(stackTrace);

打印它:print(DiagnosticsStackTrace('Stack', stackTrace).getProperties());这样你就不会受到颤振的干扰,并且可以处理所有的错误属性。

最新更新