#dart #asynchronous #async-await
#dart #асинхронный #асинхронный -ожидание
Вопрос:
Мой метод ProcessData() выполняется до завершения pullAllData(), но мне нужно, чтобы ProcessData() дождался полного завершения pullAllData() перед запуском. Это приводит к тому, что мой isdownloadsucccessful bool становится нулевым при запуске ProcessData() .
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=$periodValueamp;apikey=$apiKey';
http.Response historicalResponse = await http.get(historicalRequestURL);
isPullSuccessful = (historicalResponse.statusCode == 200);
}
void processData(cryptoCurrency) {
if (isPullSuccessful) {
...
} else {
throw 'Problem pulling data';
}
}
Ответ №1:
Вы помечаете свою функцию pullTimesAndPrices
как async
, но не используете await
. await
Перед вызовом pullAllData
функции используйте ключевое слово.
Комментарии:
1. Потрясающее спасибо! Я изменил свой код на Future<void> pullTimesAndPrices() async { for (String cryptoCurrency в cryptoAbbreviation) { ожидание pullAllData(криптовалюта); ProcessData(криптовалюта); } }