ListTiles列表中的重复条目- Flutter

pdtvr36n  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(113)

我试图从我的界面向“购物车”列表中添加物品,并看到对于特定的物品,添加不同的物品会产生重复的条目。

List<Widget> _subSubExpansionTiles(BuildContext context, List<Item> itemSubList) {
// this makes the second level of ExpansionTiles - the item , i.e. "Regular", "Starch" etc
List<Widget> _subSubList = [];

var groupedActionLists = groupBy(itemSubList, (Item item) => item.service);
//print(groupedActionLists.keys.toList());
Map<String, Map<String, dynamic>> counter = {};

print("*************************${groupedActionLists.keys} ****************************************");
for(var key in groupedActionLists.keys) {   // Wash And Fold, Wash And Iron
  //print("$key : ${groupedActionLists[key]!.length.toString()}");
  print("*************$key : ${groupedActionLists[key]!.length} ******************");
  Map<String, dynamic> subMap = {};
  int regular = 0;
  int regularStarch = 0;
  int stainRemoval = 0;
  int stainRemovalStarch = 0;
  for(Item item in groupedActionLists[key]!) {
    if (item.starch == null && item.stainRemoval == null) {
      regular++;
    }
    else if (item.starch != null && item.stainRemoval == null) {
      if (item.starch == false) {
        regular++;
      } else {
        regularStarch++;
      }
    }
    else if (item.starch == null && item.stainRemoval != null) {
      if (item.stainRemoval == false) {
        regular++;
      } else {
        stainRemoval++;
      }
    }
    else if (item.starch != null && item.stainRemoval != null) {
      if (item.starch == false && item.stainRemoval == false) {
        regular++;
      } else if (item.starch == true && item.stainRemoval == false) {
        regularStarch++;
      } else if (item.starch == false && item.stainRemoval == true) {
        stainRemoval++;
      } else if (item.starch == true && item.stainRemoval == true) {
        stainRemovalStarch++;
      }
    }

    subMap.addEntries({'regular' : regular}.entries);
    subMap.addEntries({'regularStarch' : regularStarch}.entries);
    subMap.addEntries({'stainRemoval' : stainRemoval}.entries);
    subMap.addEntries({'stainRemovalStarch' : stainRemovalStarch}.entries);

    counter.addEntries({item.service! : subMap}.entries);
  };
  //print(counter);
  String prevKey = "";
  for(var key in counter.keys){
    print("Iterating through $key");
    for (var subkey in counter[key]!.keys) {
      //print("${counter[key]!.keys}");
      String nameString = '';
      if (subkey == 'regular')
        nameString = key;
      else if (subkey == 'regularStarch')
        nameString = key + '+ Starch';
      else if (subkey == 'stainRemoval')
        nameString = key + '+ Stain Removal';
      else if (subkey == 'stainRemovalStarch') nameString = key + '+ Stain Removal + Starch';
      if (counter[key]![subkey] > 0) {
        print("adding : $nameString");
        _subSubList.add(ListTile(
            title: Text(nameString),
            visualDensity: VisualDensity.compact,
            leading: Padding(padding: EdgeInsets.fromLTRB(5, 0, 5, 0)),
            trailing: Text(
              counter[key]![subkey].toString(),
              style: const TextStyle(
                fontSize: 15,
              ),
            )));
      }
      //print("$key : ${counter[key]![subkey]}");
    }
  }
}
//print("returning $_subSubList");
return (_subSubList);

}
我得到以下输出,其中我们可以看到“Wash and Iron”重复了两次。匡威亦然-当我先添加洗涤和折叠,然后添加洗涤和熨烫时,我看到洗涤和折叠两次。
在这里,Item类具有以下成员:

class Item implements Jsonifyable {
  String? id;
  String? item;
  String? image;

  String? action;

  String? service;
  bool? starch;
  bool? stainRemoval;

  Item({
    this.id,
    this.item,
    this.image,
    this.action,
    String? service,
    this.starch,
    this.stainRemoval,
  })

我添加了调试语句来理解发生了什么,我得到:

Reloaded 8 of 1506 libraries in 1,799ms (compile: 202 ms, reload: 546 ms, reassemble: 1026 ms).
I/flutter (10344): *************************(Wash and Iron, Wash and Fold) ****************************************
I/flutter (10344): *************Wash and Iron : 1 ******************
I/flutter (10344): Iterating through Wash and Iron
I/flutter (10344): adding : Wash and Iron
I/flutter (10344): *************Wash and Fold : 1 ******************
I/flutter (10344): Iterating through Wash and Iron
I/flutter (10344): adding : Wash and Iron
I/flutter (10344): Iterating through Wash and Fold
I/flutter (10344): adding : Wash and Fold

这里_subsublist函数由以下函数调用:

List<Widget> _subExpansionTiles(BuildContext context, List<Item> itemList) {
    // this makes the first level of ExpansionTiles - the item , i.e. "Shirt", "Trouser" etc
    String action = itemList[0].action!;
    List<Widget> itemTiles = [];
    var groupedIdLists = groupBy(itemList, (Item item) => item.id);
    // we get {'S001' : [], 'S002' : [] ... etc ... } 
    groupedIdLists.forEach((key, value) {
      List<Item> _subItemList = value;
      String itemName = _subItemList[0].item!;
      var tile = ExpansionTile(
          title: Text(itemName),
          subtitle: Text('Click to expand'),
          controlAffinity: ListTileControlAffinity.leading,
          maintainState: true,
          trailing: Text(
            _subItemList.length.toString(),
            style: const TextStyle(
              fontSize: 20,
            ),
          ),
          children: _subSubExpansionTiles(context, _subItemList));
      itemTiles.add(tile);
    });
    return itemTiles;
  }

  Widget _buildExpansionTiles(BuildContext context, String category) {
    // this makes the first level of ExpansionTiles - the item , i.e. "Iron", "Dry Clean", etc
    if (!cartItems.containsKey(category)) {
      return SliverToBoxAdapter(child: Container());
    }
    List<Item> _categoryItems = cartItems[category]!;
    if (_categoryItems.length == 0) return SliverToBoxAdapter(child: Container());
    String action = _categoryItems[0].action!;
    return SliverToBoxAdapter(
      child: ExpansionTile(
        title: Text(action),
        subtitle: Text('Click to expand'),
        controlAffinity: ListTileControlAffinity.trailing,
        maintainState: true,
        leading: Text(
          _categoryItems.length.toString(),
          style: const TextStyle(
            fontSize: 25,
          ),
        ),
        children: _subExpansionTiles(context, _categoryItems),
      ),
    );
  }

有人能帮我弄清楚为什么有重复的条目吗?

kcwpcxri

kcwpcxri1#

我不是很确定,但是我相信你需要移动一下线

Map<String, Map<String, dynamic>> counter = {};

到底线以下

for(var key in groupedActionLists.keys) {   // Wash And Fold, Wash And Iron

因为现在当它到达循环中的第二个键时,counter仍然由前一个循环填充。通过在循环中移动它,它从一个空计数器开始。

相关问题