我的方法processData()是在pullAllData()完成之前执行的,但是我需要processData()在运行之前等待pullAllData()完全完成。当运行isDownloadSuccessful ()时,这将导致我的processData bool为空。
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';
}
}发布于 2021-01-14 22:44:23
您正在将函数pullTimesAndPrices标记为async,但不使用await。在调用await函数之前使用pullAllData关键字。
https://stackoverflow.com/questions/65727863
复制相似问题