# readLine method

1. @override

Future<String?> readLine()
override

Reads a line of text.

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

## Returns

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

## Example

    final reader = FileReader('lines.txt');
    try {
      String? line;
      int lineNumber = 1;
      while ((line = await reader.readLine()) != null) {
        print('Line $lineNumber: $line');
        lineNumber++;
      }
    } finally {
      await reader.close();
    }

Throws IOException if an I/O error occurs. Throws StreamClosedException if the reader has been closed.

## Implementation

    @override
    Future<String?> readLine() async {
      checkClosed();

      final line = StringBuffer();
      bool foundLine = false;

      while (true) {
        // Ensure we have data in buffer
        if (_position >= _count) {
          await _fillBuffer();
          if (_count == 0) {
            // End of stream
            break;
          }
        }

        // Look for line terminator in current buffer
        int lineEnd = -1;
        bool foundCR = false;

        for (int i = _position; i < _count; i++) {
          final char = _buffer[i];
          if (char == 10) { // '\n'
            lineEnd = i;
            foundLine = true;
            break;
          } else if (char == 13) { // '\r'
            lineEnd = i;
            foundCR = true;
            foundLine = true;
            break;
          }
        }

        if (foundLine) {
          // Add characters up to line terminator
          for (int i = _position; i < lineEnd; i++) {
            line.writeCharCode(_buffer[i]);
          }

          _position = lineEnd + 1;

          // Handle '\r\n' sequence
          if (foundCR && _position < _count && _buffer[_position] == 10) {
            _position++; // Skip the '\n'
          } else if (foundCR && _position >= _count) {
            // Need to check next buffer for '\n'
            await _fillBuffer();
            if (_count > 0 && _buffer[0] == 10) {
              _position = 1;
            } else {
              _position = 0;
            }
          }

          return line.toString();
        } else {
          // No line terminator found, add all remaining characters in buffer
          for (int i = _position; i < _count; i++) {
            line.writeCharCode(_buffer[i]);
          }
          _position = _count;
        }
      }

      // Return what we have, or null if nothing was read
      return line.isEmpty ? null : line.toString();
    }