所以我有这个显示股票价格的小工具。我使用HTTP调用来获取股票价格,当然此功能是异步的,并且在主体中,我等待网络服务完成后再返回股票价格。但是,每次我尝试打印此股票价格时,它都说这是一个未来,但我不知道如何拆封它?我已经在网络通话中等待着,所以不应该拆封它并让我获得真正的价值吗?
StockService stockService = new StockService();
var stockPrice = stockService.getStockPriceBySymbol(symbols.elementAt(x));
print(stockPrice);
NetworkServices:
Future<dynamic> getStockPriceBySymbol(String symbol) async {
NetworkService networkService = NetworkService(
'$alphaStockApiURL?function=GLOBAL_QUOTE&symbol=$symbol&apikey=$kAlphaStockAPIKey');
var stockData = await networkService.getData();
// print(stockData['Global Quote']['05. price']);
return stockData['Global Quote']['05. price'];
}
答案 0 :(得分:0)
您需要等待stockService.getStockPriceBySymbol
var stockPrice = await stockService.getStockPriceBySymbol(symbols.elementAt(x));
或将来的.then
stockService.getStockPriceBySymbol(symbols.elementAt(x)).then((stockPrice){
print(stockPrice);
)};