【发布时间】:2021-01-15 07:06:57
【问题描述】:
我的方法 processData() 在 pullAllData() 完成之前执行,但我需要 processData() 等到 pullAllData () 在运行之前完全完成。当运行 processData() 时,这导致我的 isDownloadSuccessful bool 为 Null。
Future getCoinData() async {
calculateNumberOfDataPoints();
pullTimesAndPrices();
return timesAndPrices;
}
Future pullTimesAndPrices() async {
for (String cryptoCurrency in cryptoAbbreviation) {
pullAllData(cryptoCurrency);
processData(cryptoCurrency);
}
}
Future pullAllData(cryptoCurrency) async {
String historicalRequestURL =
'$cryptoAPIURL$cryptoCurrency$currency/ohlc?periods=$periodValue&apikey=$apiKey';
http.Response historicalResponse = await http.get(historicalRequestURL);
isPullSuccessful = (historicalResponse.statusCode == 200);
}
void processData(cryptoCurrency) {
if (isPullSuccessful) {
...
} else {
throw 'Problem pulling data';
}
}
【问题讨论】:
标签: dart asynchronous async-await