flutter 如何< double>在具有Provider的ListView中返回Future?

kgsdhlau  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(114)

我尝试使用Provider包将Future<double>放入列表视图,但没有成功。有人能给予我一些建议或链接,解释如何在返回双精度型的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<double>的示例“而不是值。


的数据

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,
                ),
              ),
            );
          },
        );
      },
    );
  }
}

yjghlzjz

yjghlzjz1#

谢谢,我想我用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,
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }
}

字符串

相关问题