简体   繁体   English

如何处理“添加到应用程序”Flutter Android 应用程序中的异步错误

[英]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.

相关问题 如何使用 Flutter(在 Android 和 iOS 上)处理应用程序生命周期? - How to handle app lifecycle with Flutter (on Android and iOS)? 如何为 Flutter 应用添加“.in”域以避免 Android 资源链接错误? - How to add '.in' domain for flutter app to avoid Android resource linking error? 如何在 flutter 应用程序中处理或覆盖 android 设备后退按钮的操作? - How to handle or override the action of android device back button in flutter application? 如何将原生 android 屏幕添加到 flutter 应用程序? - How to add native android screen to flutter app? 如何将 Flutter 添加到现有或新的 Android 应用程序 - How Add Flutter To Existing Or New Android App 如何在应用栏中添加资产图像作为 Flutter 应用程序中的操作图标? - How to add asset image in app bar as an action icon in Flutter application? 如何在原生 Android 应用程序中添加 Flutter 模块作为按需模块 - How to Add a Flutter Module as a On Demand Module in Native Android Application <asynchronous suspension> Flutter 中的错误</asynchronous> - <asynchronous suspension> Error in Flutter Flutter,Android 应用(随机误差) - Flutter, Android application(random error) 如何在 flutter android 应用程序中向 firebse 测试实验室添加凭据以进行自定义登录 - How to add credentials to firebse testlab for custom signin in flutter android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM