简体   繁体   English

在 Flutter 中捕获未处理的异常

[英]Catching unhandled exception in Flutter

I'm trying to catch all unhandled exceptions in my Flutter app.我试图在我的 Flutter 应用程序中捕获所有未处理的异常。 Here's my main.dart:这是我的 main.dart:

void main() async {
  FlutterError.onError = (FlutterErrorDetails details) {
    print("FlutterError.onError");
  };

  try {
    runZoned<Future<void>>(
      () async {
        runApp(App());
      },
      onError: (dynamic error, StackTrace stackTrace) {
        print("runZoned.onError");
      },
    );
  } catch (ex) {
    print("main.catch");
  }
}

... however, none of these methods works, and my app outputs the following when running in the iOS simulator: ...但是,这些方法都不起作用,我的应用程序在 iOS 模拟器中运行时输出以下内容:

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 61, address = localhost, port = 57562

... how do I catch this exception? ...我如何捕捉这个异常?

OK I've solved this.好的,我已经解决了这个问题。 Some of my initialisation logic was outside the runZoned block.我的一些初始化逻辑在runZoned块之外。 I incorrectly assumed that all that was needed inside runZoned was the call to runApp .我错误地认为runZoned中所需要的runZonedrunApp的调用。 I've put everything inside the runZoned and it's now correctly catching the errors.我已将所有内容都放在runZoned ,现在它可以正确地捕获错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM