docComment property Null safety

String? docComment

The subset of lines in text that are marked as part of the documentation comments by beginning with '///'.

The leading slashes and space on each line is removed. Returns null when there is no documentation comment.

Implementation

String? get docComment {
  var buffer = StringBuffer();
  for (var line in text.split('\n')) {
    var scanner = StringScanner(line.trim());
    if (!scanner.scan('///')) continue;
    scanner.scan(' ');
    buffer.writeln(scanner.rest);
  }
  var comment = buffer.toString().trimRight();

  return comment.isNotEmpty ? comment : null;
}