2

我正在使用 Dart VM version: 1.24.3 (Wed Dec 13 23:26:59 2017) on "macos_x64" WITH Flutter 0.2.8 • channel beta Framework • revision b397406561 (13 days ago) Engine • revision c903c217a1 Tools • Dart 2.0.0-dev.43.0.flutter-52afcba357

ReadAsStringSync给我一个空字符串作为输出而不是所需的文件内容,我的代码是

var appl = new File('a.txt').readAsStringSync();
print(appl);

我也需要 readAsString 方法的帮助,因为它返回 a Future<String>,我想知道是否有任何方法或方法可以将其转换为String

4

1 回答 1

2
Future<String> readFile(String path) {
  return new File(path).readAsString();
}

void foo() async {
  var result = await readFile(path);
  print(result);
}

或者

String readFile(String path) async {
  var result = await new File(path).readAsString();
  print(result);
  return result;
}
于 2018-04-16T05:52:58.423 回答