formatStackTrace method Null safety

String? formatStackTrace(
  1. {bool showPath = false,
  2. int methodCount = 10,
  3. int skipFrames = 0}
)

Outputs a formatted string of the current stack_trace_nj showing upto methodCount methods in the trace. methodCount defaults to 10.

Implementation

String? formatStackTrace(
    {bool showPath = false, int methodCount = 10, int skipFrames = 0}) {
  var formatted = <String>[];
  var count = 0;

  for (var stackFrame in frames!) {
    if (skipFrames > 0) {
      skipFrames--;
      continue;
    }
    String sourceFile;
    if (showPath) {
      sourceFile = stackFrame.sourceFile.path;
    } else {
      sourceFile = basename(stackFrame.sourceFile.path);
    }
    var newLine =
        ('$sourceFile : ${stackFrame.details} : ${stackFrame.lineNo}');

    if (_workingDirectory != null) {
      formatted.add('file:///$_workingDirectory$newLine');
    } else {
      formatted.add(newLine);
    }
    if (++count == methodCount) {
      break;
    }
  }

  if (formatted.isEmpty) {
    return null;
  } else {
    return formatted.join('\n');
  }
}