dartcommand-line-interfacedart-io

How to disable stdin echo in a dart console application for password input


The example below will read in a user's password but also echo it in plain text, is there a way around this?

Future<String> promptPassword() {
  var completer = new Completer<String>();
  var stream = new StringInputStream(stdin);
  stdout.writeString("Warning: Password will be displayed here until I find a better way to do this.\n");
  stdout.writeString("Watch your back...\n");
  stdout.writeString("GitHub password for $gituser: ");
  stream.onLine = () {
    var str = stream.readLine();
    stdin.close();
    completer.complete(str);
  };
  return completer.future;
}

Solution

  • This has been implemented now.

    See Stdin.echoMode

    import 'dart:io';
    
    void main() {
      print('Enter password: ');
      stdin.echoMode = false;
      var password = stdin.readLineSync();  
      // Do something with password...
    }