0

我想ListTile在点击 ListTile 时增加尾随 Flutter 的数字,
但总是返回 0?
我正在使用future builder fyi,在此先感谢

这是我的模型

class ItemModel {
  String name;
  String price;
  String image;
  bool isSelected = false;
  int countSelected = 0;

  ItemModel(
    this.name,
    this.price,
    this.image,
  );
}

此方法从本地服务器获取 api json 数据

  Future<List<ItemModel>> _getItems() async {
    List<ItemModel> listItems = [];
    var url = Uri.parse(BASEURLLOCAL.apiGetItems);
    var data = await http.get(url);
    var jsonData = jsonDecode(data.body);

    for (var p in jsonData) {
      ItemModel item = ItemModel(
        p["name"],
        p["price"],
        p["image"],
        // p["PRODUCT_NAME"],
        // p["PRICE_SELL"],
        // p["FILENAME"],
      );
      listItems.add(item);
    }
    return listItems;
  }

这是列表视图生成器

  @override
  Widget build(BuildContext context) {
    
    return FutureBuilder<List<ItemModel>>(
      future: _getItems(),
      builder: (context, snapshot) {
        if (snapshot.data == null) {
          return Container(
            child: Center(
              child: CircularProgressIndicator(),
            ),
          );
        } else {
          return ListView.builder(
            shrinkWrap: true,
            itemCount: snapshot.data.length,
            itemBuilder: (BuildContext contex, int idx) {
              if (_itemCount.length < snapshot.data.length) {
                _itemCount.add(0);
              }
              return Card(
                child: ListTile(
                  leading: Image.network(
                      BASEURLLOCAL.imgItem + snapshot.data[idx].image),
                  title: Text(snapshot.data[idx].name),
                  subtitle: Text(snapshot.data[idx].price),
                  // trailing: _buildTrailing(),
                  trailing: Text(snapshot.data[idx].countSelected.toString()),
                  onTap: () {
                    setState(() {
                      snapshot.data[idx].countSelected++;
                    });
                  },
                ),
              );
            },
          );
        }
      },
    );
  }

这就是问题所在

4

1 回答 1

0

尝试这样的事情,创建一个列表

List<ItemModel> listItems = [];

然后,在initState()调用_getItems()

_getItems() async {
    var url = Uri.parse(BASEURLLOCAL.apiGetItems);
    var data = await http.get(url);
    var jsonData = jsonDecode(data.body);

    for (var p in jsonData) {
      ItemModel item = ItemModel(
        p["name"],
        p["price"],
        p["image"],
        // p["PRODUCT_NAME"],
        // p["PRICE_SELL"],
        // p["FILENAME"],
      );
      listItems.add(item);
    }
  }

并删除FutureBuilder

@override
  Widget build(BuildContext context) {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: listItems.length,
      itemBuilder: (BuildContext context, int idx) {
        if (_itemCount.length < listItems.length) {
          _itemCount.add(0);
        }
        return Card(
          child: ListTile(
            leading: Image.network(BASEURLLOCAL.imgItem + listItems[idx].image),
            title: Text(listItems[idx].name),
            subtitle: Text(listItems[idx].price),
            // trailing: _buildTrailing(),
            trailing: Text(listItems[idx].countSelected.toString()),
            onTap: () {
              setState(() {
                listItems[idx].countSelected++;
              });
            },
          ),
        );
      },
    );
  }

通过这种方式,它不会在每次重建时都获取。

于 2021-10-09T08:24:25.007 回答