如何在带有 Provider 的 ListView 中返回 Future<double>?
Posted
技术标签:
【中文标题】如何在带有 Provider 的 ListView 中返回 Future<double>?【英文标题】:How do I return a Future<double> in a ListView with Provider? 【发布时间】:2020-12-27 21:57:51 【问题描述】:我正在尝试使用 Provider 包将 Future 放入列表视图,但没有成功。谁能给我一些建议或链接来解释如何在返回双精度的 Future 上实现 notifylisteners()?
这是 getPrice() 的未来:
import 'dart:collection';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:stock_watchlist/stock_service.dart';
class StockData extends ChangeNotifier
List<Stock> _stocks = [
Stock(symbol: 'AAPL'),
Stock(symbol: 'AMD'),
Stock(symbol: 'TSLA')
];
UnmodifiableListView<Stock> get stocks
return UnmodifiableListView(_stocks);
int get stockCount
return _stocks.length;
void addStock(String symbol, double price, double eps)
final stock = Stock(symbol: symbol);
_stocks.add(stock);
notifyListeners();
void deleteStock(Stock stock)
_stocks.remove(stock);
notifyListeners();
Future<double> getPrice(String symbol) async
String url =
"https://sandbox.iexapis.com/stable/stock/$symbol/quote/latestPrice?token=Tsk_38ddda0b877a4510b42a37ae713cdc96";
http.Response response = await http.get(url);
double price = double.tryParse(response.body);
return price;
Future<double> getEps(String symbol) async
String url =
"https://sandbox.iexapis.com/stable/stock/$symbol/stats/ttmEPS?token=Tsk_38ddda0b877a4510b42a37ae713cdc96";
http.Response response = await http.get(url);
double eps = double.tryParse(response.body);
return eps;
这是我尝试将 getPrice() 的值输入到文本小部件的地方,但它给了我“'Future' 的实例而不是值。
I cannot embed pictures yet
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:stock_watchlist/constants.dart';
import 'package:stock_watchlist/stock_model.dart';
class StockList extends StatelessWidget
@override
Widget build(BuildContext context)
return Consumer<StockData>(
builder: (context, stockData, child)
return ListView.builder(
itemCount: stockData.stockCount,
itemBuilder: (context, index)
//
final stockIndex = stockData.stocks[index];
//
return ListTile(
title: Text(
stockIndex.symbol,
style: TextStyle(
color: kTextColor,
),
),
subtitle: Text(
stockIndex.price.toString(),
style: TextStyle(
color: kTextColor,
),
),
);
,
);
,
);
【问题讨论】:
你试过FutureBuilder
吗?您可以尝试使构建器功能async
然后final stockIndex = await stockData.stocks[index]
。
【参考方案1】:
谢谢,我想我是用 FutureBuilder 解决的。这是我使用的代码。
class StockList extends StatelessWidget
@override
Widget build(BuildContext context)
return Consumer<StockData>(
builder: (context, stockData, child)
return ListView.builder(
itemCount: stockData.stockCount,
itemBuilder: (context, index)
//
final stockIndex = stockData.stocks[index];
//
return ListTile(
title: Text(
stockIndex.symbol,
style: TextStyle(
color: kTextColor,
),
),
subtitle: FutureBuilder(
future: stockData.getPrice(stockIndex.symbol),
builder: (context, snapshot) => Text(
snapshot.data.toString(),
style: TextStyle(
color: kTextColor,
),
),
),
);
,
);
,
);
【讨论】:
以上是关于如何在带有 Provider 的 ListView 中返回 Future<double>?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ListView Widget 中使用 markdown 包(对于带有项目符号的文本)?在 ListView 中使用时没有显示