上述问题是在 Dart Google+ 社区提出的,但没有给出明确的答案,所以我想我应该在这里重复这个问题,因为我真的很想知道。以下是 Dart 社区的帖子:
https://plus.google.com/u/0/103493864228790779294/posts/U7VTyX5h7HR
那么,在有或没有错误处理的情况下,执行此操作的正确方法是什么?
您链接到的问题是关于异步读取多个文件的内容,这是一个更难的问题。我认为弗洛里安的解决方案没有问题。简化一下,这似乎成功异步读取文件:
import 'dart:async';
import 'dart:io';
void main() {
new File('/home/darshan/so/asyncRead.dart')
.readAsString()
..catchError((e) => print(e))
.then(print);
print("Reading asynchronously...");
}
Run Code Online (Sandbox Code Playgroud)
这输出:
异步读取...
导入'dart:异步';
导入 'dart:io';
无效主(){
新文件('/home/darshan/so/asyncRead.dart')
.readAsString()
..catchError((e) => 打印(e))
.然后(打印);
print("异步读取...");
}
作为记录,以下是 Florian Loitsch 对最初问题的解决方案(稍作修改):
Reading asynchronously...
import 'dart:async';
import 'dart:io';
void main() {
new File('/home/darshan/so/asyncRead.dart')
.readAsString()
..catchError((e) => print(e))
.then(print);
print("Reading asynchronously...");
}
| 归档时间: |
|
| 查看次数: |
1220 次 |
| 最近记录: |