CuriousCoder
CuriousCoder

Reputation: 262

Method running before previous method is finished, Future Async Dart

My method processData() is executing before pullAllData() is finished but I need processData() to wait until pullAllData() is completely finished before running. This is causing my isDownloadSuccessful bool to be Null when processData() is ran.

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';
    }
  }

Upvotes: 0

Views: 38

Answers (1)

pasty
pasty

Reputation: 420

You are marking your function pullTimesAndPrices as async but not using await. Use the await keyword before calling the pullAllData function.

Upvotes: 1

Related Questions