gpt4 book ai didi

dart - 如何在Dart中异步读取文件?

转载 作者:行者123 更新时间:2023-12-03 03:09:07 24 4
gpt4 key购买 nike

上面的问题是在Dart Google+社区提出的,没有给出明确的答案,所以我想在这里重复这个问题,因为我真的很想知道。这是Dart社区的帖子:

https://plus.google.com/u/0/103493864228790779294/posts/U7VTyX5h7HR

那么有无错误处理的正确方法是什么呢?

最佳答案

您链接到的问题是关于异步读取多个文件的内容,这是一个较难的问题。我认为Florian的解决方案没有问题。简化一下,这似乎可以成功地异步读取文件:

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...");
}

输出:
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...");}

For the record, here's Florian Loitsch's (slightly modified) solution to the initial problem:

import 'dart:async';
import 'dart:io';

void main() {
new Directory('/home/darshan/so/j')
.list()
.map((f) => f.readAsString()..catchError((e) => print(e)))
.toList()
.then(Future.wait)
.then(print);

print("Reading asynchronously...");
}

关于dart - 如何在Dart中异步读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16030581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com