splitLines function

List<String> splitLines(
  1. String text, {
  2. bool preserveNewlines = false,
})

Split text into lines.

If preserveNewlines is true, trailing newline characters are kept on each line.

Implementation

List<String> splitLines(String text, {bool preserveNewlines = false}) {
  if (preserveNewlines) {
    final result = <String>[];
    final re = RegExp(r'[^\n]*\n?');
    for (final m in re.allMatches(text)) {
      final s = m.group(0)!;
      if (s.isNotEmpty) result.add(s);
    }
    return result;
  }
  return text.split('\n');
}