FlutterErrorDetail 如何在 Flutter 中获取错误的类名?

huangapple go评论64阅读模式
英文:

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&lt;CustomError&gt; createState() =&gt; _CustomErrorState();
}

class _CustomErrorState extends State&lt;CustomError&gt; {


  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Card(
        child: Padding(
          child: Text(
            &quot;Something is not right here...&quot;,
            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(
    &quot;Something is not right here.\nStack trace: ${widget.errorDetails.stack?.toString() ?? &#39;(no stack trace stored in error)&#39;}&quot;,
    style: const TextStyle(
        color: Colors.white,
        fontWeight: FontWeight.bold,
    ),
),

huangapple
  • 本文由 发表于 2023年7月28日 02:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定