debugPrintStack function Null safety

void debugPrintStack(
  1. {StackTrace? stackTrace,
  2. String? label,
  3. int? maxFrames}
)

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

If the stackTrace parameter is null, the StackTrace.current is used to obtain the stack.

The maxFrames argument can be given to limit the stack to the given number of lines before filtering is applied. By default, all stack lines are included.

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

Implementation

void debugPrintStack({StackTrace? stackTrace, String? label, int? maxFrames}) {
  if (label != null) {
    debugPrint(label);
  }
  if (stackTrace == null) {
    stackTrace = StackTrace.current;
  } else {
    stackTrace = FlutterError.demangleStackTrace(stackTrace);
  }
  Iterable<String> lines = stackTrace.toString().trimRight().split('\n');
  if (kIsWeb && lines.isNotEmpty) {
    // Remove extra call to StackTrace.current for web platform.
    // TODO(ferhat): remove when https://github.com/flutter/flutter/issues/37635
    // is addressed.
    lines = lines.skipWhile((String line) {
      return line.contains('StackTrace.current') ||
             line.contains('dart-sdk/lib/_internal') ||
             line.contains('dart:sdk_internal');
    });
  }
  if (maxFrames != null) {
    lines = lines.take(maxFrames);
  }
  debugPrint(FlutterError.defaultStackFilter(lines).join('\n'));
}