如何在dart控制台应用程序中禁用stdin echo

时间:2013-01-29 20:10:27

标签: dart dart-io

下面的示例将读取用户密码,但也会以纯文本形式回显它,有没有办法解决这个问题?

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;
}

2 个答案:

答案 0 :(得分:4)

在将tty控件添加到dart:io之前,这是不可能的。与此同时,我建议:

stty -echo
dart ./password.dart
stty echo

我在这里打开了一个错误:https://code.google.com/p/dart/issues/detail?id=8190

答案 1 :(得分:4)

现在已经实施了。

请参阅Stdin.echoMode

import 'dart:io';

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