betterPrint function
A better way to show print message
maxLine
control how many line to show after message.
Implementation
void betterPrint(Object? message, [int maxLine = 0]) {
final msg = message.toString();
final stackTrace = StackTrace.current;
Iterable<String> lines = stackTrace.toString().trimRight().split('\n');
// removing [betterPrint] from stack
String line = lines.toList()[1];
final i = line.indexOf('package:');
if (maxLine > 0) {
lines = lines.take(maxLine + (i < 0 ? 2 : 1) + 1);
final ls = lines.toList()..removeRange(0, i < 0 ? 2 : 1);
final l = ls.toList().map((e) {
final x = e.split(' ');
final i = int.parse(x.first.substring(1));
x.replaceRange(0, 1, ['#${i - 1}']);
return x.join(' ');
}).toList();
debugPrint(
FlutterError.defaultStackFilter(l)
.join('\n')
.replaceAll('packages/', 'package:')
.replaceRange(0, 2, msg),
);
} else {
final list = line.split(" ");
debugPrint('$msg ${list.last}');
}
}