readTextLines method

  1. @override
Future<Result<List<String>, FileError>> readTextLines(
  1. String path, {
  2. int? maxLines,
})
override

Reads UTF-8 text lines. Implementations should stop once maxLines lines have been read.

Implementation

@override
Future<Result<List<String>, FileError>> readTextLines(
  String path, {
  int? maxLines,
}) async {
  if (maxLines != null && maxLines <= 0) return const Ok([]);
  final resolved = _resolve(path);
  try {
    final lines = <String>[];
    final stream = File(
      resolved,
    ).openRead().transform(utf8.decoder).transform(const LineSplitter());
    await for (final line in stream) {
      lines.add(line);
      if (maxLines != null && lines.length >= maxLines) break;
    }
    return Ok(lines);
  } on Object catch (error) {
    return Err(_toFileError(error, resolved));
  }
}