首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法在前面的方法完成之前运行,未来异步Dart

方法在前面的方法完成之前运行,未来异步Dart
EN

Stack Overflow用户
提问于 2021-01-14 22:32:39
回答 1查看 22关注 0票数 0

我的方法processData()是在pullAllData()完成之前执行的,但是我需要processData()在运行之前等待pullAllData()完全完成。当运行isDownloadSuccessful ()时,这将导致我的processData bool为空。

代码语言:javascript
复制
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';
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-14 22:44:23

您正在将函数pullTimesAndPrices标记为async,但不使用await。在调用await函数之前使用pullAllData关键字。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65727863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档