toString method

  1. @override
String toString()
override

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation

@override
String toString() {
  const title = 'LeakTrackerException: Native memory leaks detected';
  final sorted = [...leaks]..sort((a, b) => b.size.compareTo(a.size));
  final rule = '─' * 40;

  final buffer = StringBuffer()
    ..writeln(red(bold(title)))
    ..writeln('Filter  : ${bold('$filter')}')
    ..writeln('Leaks   : ${yellow(bold('${sorted.length}'))}')
    ..writeln('Total   : ${red(bold(formatBytes(totalBytes)))}')
    ..writeln('Largest : ${red(bold(formatBytes(sorted.first.size)))}')
    ..writeln(rule);

  for (final (i, leak) in sorted.indexed) {
    buffer
      ..writeln(
        yellow('Leak #${i + 1} - ${leak.type} (${formatBytes(leak.size)})'),
      )
      ..writeln('  address   : ${cyan(formatAddress(leak.address))}')
      ..writeln('  timestamp : ${blue(leak.timestamp.toIso8601String())}')
      ..writeln('  stack     :');

    for (final line in leak.stack.trimRight().split('\n')) {
      buffer.writeln('    ${dim(line)}');
    }

    buffer.writeln(rule);
  }

  return buffer.toString();
}