dictionary - How to use a map to fire async request but get an aggregated result? -
i have code:
import 'dart:async'; future<int> expensivecallfromalib(int value) async { print('expensivecall($value)'); return value + 1; } test() { map<string, int>input = {"one":1, "two":2}; map result = {}; print("a"); input.foreach((string key, int value) { expensivecallfromalib(value).then((int value) { result[key] = value; }); print("b"); }); print("c"); print(result); } main() { test(); }
... output
a b b c {} expensivecall(1) expensivecall(2)
... want
a b expensivecall(1) b expensivecall(2) c {one: 2, two: 3}
the point is, cant change expensivecallfromalib method.
test() async { map<string, int>input = {"one":1, "two":2}; map result = {}; print("a"); for(final key in input.keys) { int value = await expensivecallfromalib(input[key]); result[key] = value; print("b"); } print("c"); print('result: $result'); }
the output not how want guess it's close enough ;-)
html output console expensivecall(1) b expensivecall(2) b c result: {one: 2, two: 3}
Comments
Post a Comment