12

Flutter/Dart 获取当前的 stacktrace - 技术小黑屋

 3 years ago
source link: https://droidyue.com/blog/2021/12/09/how-to-get-current-stacktrace-in-dart-flutter/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Flutter/Dart 获取当前的 Stacktrace

Dec 9th, 2021

出现异常时获取 stacktrace

void _printException() {
 try {
   1 ~/ 0;
 } catch (e, s) {
   print('_printException $e; $s');
 }
}

对应的 stacktrace 日志信息

_printException IntegerDivisionByZeroException; #0      int.~/ (dart:core-patch/integers.dart:30:7)
#1      _printException (file:///Users/androidyue/Documents/self_host/dart_current_stacktrace/bin/dart_current_stacktrace.dart:10:7)
#2      main (file:///Users/androidyue/Documents/self_host/dart_current_stacktrace/bin/dart_current_stacktrace.dart:3:3)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

非异常出现时获取 stacktrace

如果只是想获取当前 stacktrace,比如用来确定某些方法的执行调用场景。

  1. 可以通过人为制造 异常 的方式来进行输出打印。
void _printCurrentStacktrace() {
 try {
   throw 'printCurrentStacktrace';
 } catch (e, s) {
   print('_printCurrentStacktrace;$s');
 }
}
  1. 不人为制造异常得到 stacktrace(使用StackTrace.current
void _printCurrentStacktraceV2() {
 print('_printCurrentStacktraceV2 ${StackTrace.current}');
}

droidyue_gzh_green_png.png

加入技术小黑屋官方电报群

Posted by androidyue Dec 9th, 2021

Dart, Flutter, Stacktrace, Thread

« 使用 Flutter SDK 中的 Dart SDK


Related Issues not found

Please contact @droidyuecom to initialize the comment


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK