下面的示例将读取用户密码,但也会以纯文本形式回显它,有没有办法解决这个问题?
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;
}
答案 0 :(得分:4)
在将tty控件添加到dart:io之前,这是不可能的。与此同时,我建议:
stty -echo
dart ./password.dart
stty echo
我在这里打开了一个错误:https://code.google.com/p/dart/issues/detail?id=8190
答案 1 :(得分:4)
现在已经实施了。
import 'dart:io';
void main() {
print('Enter password: ');
stdin.echoMode = false;
var password = stdin.readLineSync();
// Do something with password...
}