Flutter捕获所有未处理的异常 [英] Flutter catching all unhandled exceptions
问题描述
我正在尝试在Flutter应用程序中捕获所有未处理的异常,因此可以将其发送给崩溃报告器。在Flutter文档中有有关如何执行此操作的说明。我遵循了这些规则,并在我的应用程序中添加了两位代码来捕获异常:
I'm trying to catch all unhandled exceptions in a Flutter app so I can sent it to a crash reporter. There are instructions on how to do this in the Flutter docs. I followed those, and added two bits of code to my app to catch exceptions:
通过包装 runApp
在 runZoned
中:
runZoned<Future<void>>(
() async {
runApp(MyApp());
},
onError: (dynamic error, StackTrace stackTrace) {
print("=================== CAUGHT DART ERROR");
// Send report
},
);
通过设置 FlutterError.onError
捕获抖动错误:
Catch flutter errors by setting FlutterError.onError
:
FlutterError.onError = (FlutterErrorDetails details) {
print("=================== CAUGHT FLUTTER ERROR");
// Send report
};
但是,当我在运行时通过抛出按钮异常来测试它时:
However, when I test this at runtime by throwing an exception from a button:
throw Exception("Just testing");
例外出现在控制台中:
════════手势
aught捕获的异常═════════════════════════════════════
处理手势时引发了以下_Exception异常:异常:
只是测试抛出
时,就是堆栈:
════════ Exception Caught By gesture ═══════════════════════════════════════════════════════════════ The following _Exception was thrown while handling a gesture: Exception: Just testing When the exception was thrown, this was the stack:
...等等
但是我看不到我的打印语句的迹象(提示飞镖错误或提示颤音错误),并且在这些行上设置断点似乎从未命中,所以我认为异常处理代码无法捕获它。我想念什么吗?
But I see no sign of my print statements (CAUGHT DART ERROR or CAUGHT FLUTTER ERROR), and setting breakpoints on those lines never seems to hit, so I think my exception handling code isn't catching it. Am I missing something?
这是一个最小的可重现示例(单击该按钮会引发异常,但未按预期捕获):
Here's a minimal reproducible example (click the button, which throws an exception, but it's not caught as expected):
import 'dart:async';
import 'package:flutter/material.dart';
void main() =>
runZoned<Future<void>>(
() async {
runApp(MyApp());
},
onError: (dynamic error, StackTrace stackTrace) {
print("=================== CAUGHT DART ERROR");
// Send report
// NEVER REACHES HERE - WHY?
},
);
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
// This captures errors reported by the FLUTTER framework.
FlutterError.onError = (FlutterErrorDetails details) {
print("=================== CAUGHT FLUTTER ERROR");
// Send report
// NEVER REACHES HERE - WHY?
};
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: RaisedButton(
child: Text("Throw exception"),
onPressed: () {
throw Exception("This is a test exception");
},
),
),
),
);
}
}
推荐答案
好我知道发生了什么事。看看一些相关的Flutter问题:
Okay I figured out what's going on. Had a look at some related Flutter issues:
在调试模式下, flutter框架捕获了很多异常,打印到控制台(有时在UI本身中以红色和黄色显示),但没有重新抛出-因此它们被有效地吞噬了,您自己的代码无法捕获它们。但是,当您以发布模式进行部署时,这不会发生。因此,我的最小可重现示例在发布模式下构建时确实捕获了异常。
It looks like when in debug mode, the flutter framework catches a lot of exceptions, prints to the console (and sometimes shows in the UI itself in red and yellow), but doesn't re-throw - so they are effectively swallowed and there's no way for your own code to catch them. But, when you deploy in release mode, this doesn't happen. So my minimal reproducible example does catch exceptions when built in release mode.
这篇关于Flutter捕获所有未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!