我需要将文本写入“.txt”文件中,将其保存在变量中并将其赋予Text, 内的TextField. 这个想法是将用户输入写入“.txt”文件中,以便他可以在需要时在 .txt 上读取他所写的内容TextField。
一切正常,当我读取文件时,它会获取正确的内容,但是当我将其存储在变量中以便Text(var_name...)很好地使用它时,我在屏幕上读到的是“‘未来’的实例”。
我知道这个问题来自于对异步和未来的错误处理,但我想真正理解为什么这不起作用。
这是我的代码:
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localBio async {
final path = await _localPath;
print(path);
return File('$path/bio.txt');
}
Future<File> _write(String text, String filename) async {
final file = await _localBio;
// Write the file.
return file.writeAsString(text);
}
Future<String> _read() async {
try {
final file = await _localBio;
String body = await file.readAsString();
// Read …Run Code Online (Sandbox Code Playgroud) 我需要在 Flutter 上读写文件。
写有效,但不读,或者我认为不是,因为终端输出是flutter: Instance of 'Future<String>'.
这是什么意思?
这是代码:
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/hello.txt');
}
Future<File> writeHello() async {
final file = await _localFile;
// Write the file.
return file.writeAsString('HelloWorld');
}
Future<String> readHello() async {
try {
final file = await _localFile;
// Read the file.
return await file.readAsString();
} catch (e) {
// If encountering an error, …Run Code Online (Sandbox Code Playgroud)