我有一个集合,现在我需要遍历其中的数据,然后修改其中一个字段,但是此修改过程很耗时,并且需要异步进行,我不知道如何正确编写此代码。
这是我的假密码:
Stream<int> asynchronousNaturalsTo(int n) async* {
yield calculate(n).asStream();
}
Future<int> calculate(int i) async {
// async calculate result. maybe there is some long-running operate.
return Future.value(i + 10);
}
main() async {
// 1. I have a List that contains data needs be calculated by async.
for (int i = 0; i < 3; i++) {
// 2. calculate
var list = await asynchronousNaturalsTo(i);
// 3. finally, use the final result.
print(list);
}
}
如何编写代码? 谢谢...