String readLineSync({Encoding encoding: SYSTEM_ENCODING, bool retainNewlines: false})

Synchronously read a line from stdin. This call will block until a full line is available.

The argument encoding can be used to changed how the input should be decoded. Default is SYSTEM_ENCODING.

If retainNewlines is false, the returned String will not contain the final newline. If true, the returned String will contain the line terminator. Default is false.

If end-of-file is reached after any bytes have been read from stdin, that data is returned. Returns null if no bytes preceeded the end of input.

Source

String readLineSync({Encoding encoding: SYSTEM_ENCODING,
                     bool retainNewlines: false}) {
  const CR = 13;
  const LF = 10;
  final List<int> line = <int>[];
  // On Windows, if lineMode is disabled, only CR is received.
  bool crIsNewline = Platform.isWindows &&
      (stdioType(stdin) == StdioType.TERMINAL) &&
      !lineMode;
  if (retainNewlines) {
    int byte;
    do {
      byte = readByteSync();
      if (byte < 0) {
        break;
      }
      line.add(byte);
    } while (byte != LF && !(byte == CR && crIsNewline));
    if (line.isEmpty) {
      return null;
    }
  } else if (crIsNewline) {
    // CR and LF are both line terminators, neither is retained.
    while (true) {
      int byte = readByteSync();
      if (byte < 0) {
        if (line.isEmpty) return null;
        break;
      }
      if (byte == LF || byte == CR) break;
      line.add(byte);
    }
  } else {
    // Case having to hande CR LF as a single unretained line terminator.
    outer: while (true) {
      int byte = readByteSync();
      if (byte == LF) break;
      if (byte == CR) {
        do {
          byte = readByteSync();
          if (byte == LF) break outer;

          line.add(CR);
        } while (byte == CR);
        // Fall through and handle non-CR character.
      }
      if (byte < 0) {
        if (line.isEmpty) return null;
        break;
      }
      line.add(byte);
    }
  }
  return encoding.decode(line);
}