debugPrintStack function

void debugPrintStack ({String label, int maxFrames })

Dump the current stack to the console using debugPrint and FlutterError.defaultStackFilter.

The current stack is obtained using StackTrace.current.

The maxFrames argument can be given to limit the stack to the given number of lines. By default, all non-filtered stack lines are shown.

The label argument, if present, will be printed before the stack.

Implementation

void debugPrintStack({ String label, int maxFrames }) {
  if (label != null)
    debugPrint(label);
  Iterable<String> lines = StackTrace.current.toString().trimRight().split('\n');
  if (kIsWeb) {
    // Remove extra call to StackTrace.current for web platform.
    // TODO(ferhat): remove when https://github.com/flutter/flutter/issues/37635
    // is addressed.
    lines = lines.skip(1);
  }
  if (maxFrames != null)
    lines = lines.take(maxFrames);
  debugPrint(FlutterError.defaultStackFilter(lines).join('\n'));
}