英文:
FlutterErrorDetail How to get class name of error in Flutter?
问题
我使用这段代码来捕获我的Flutter应用程序中的错误,它完美地工作,但我想知道错误发生在哪个页面或类中。
https://api.flutter.dev/flutter/widgets/ErrorWidget-class.html
ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
return CustomError(errorDetails: errorDetails);
};
class CustomError extends StatefulWidget {
final FlutterErrorDetails errorDetails;
final Widget? page;
CustomError({
Key? key,
required this.errorDetails,
this.page,
}) : super(key: key);
@override
State<CustomError> createState() => _CustomErrorState();
}
class _CustomErrorState extends State<CustomError> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Center(
child: Card(
child: Padding(
child: Text(
"这里有些不对劲...",
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
padding: const EdgeInsets.all(8.0),
),
color: Colors.red,
margin: EdgeInsets.zero,
),
);
}
}
英文:
I'm using this code to catch errors in my flutter application and it works perfectly but I want to know the error happen in which page or class. !
https://api.flutter.dev/flutter/widgets/ErrorWidget-class.html
ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
return CustomError(errorDetails: errorDetails);
};
class CustomError extends StatefulWidget {
final FlutterErrorDetails errorDetails;
final Widget? page;
CustomError({
Key? key,
required this.errorDetails,
this.page,
}) : super(key: key);
@override
State<CustomError> createState() => _CustomErrorState();
}
class _CustomErrorState extends State<CustomError> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Center(
child: Card(
child: Padding(
child: Text(
"Something is not right here...",
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
padding: const EdgeInsets.all(8.0),
),
color: Colors.red,
margin: EdgeInsets.zero,
),
);
}
}
答案1
得分: 1
要找到错误发生的位置,您需要获取StackTrace,它应该存储在FlutterErrorDetails.stack内。您可以使用toString()
来显示它。
child: Text(
"出现问题了。\n堆栈跟踪:${widget.errorDetails.stack?.toString() ?? '(错误中未存储堆栈跟踪)'}",
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
英文:
To get where the error happened you need to get the StackTrace, it should be stored inside FlutterErrorDetails.stack. You can display it using toString()
.
child: Text(
"Something is not right here.\nStack trace: ${widget.errorDetails.stack?.toString() ?? '(no stack trace stored in error)'}",
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论