代码之家  ›  专栏  ›  技术社区  ›  kilokahn

ListTiles列表中的重复条目-Flutter

  •  1
  • kilokahn  · 技术社区  · 1 年前

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

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

    }

    我得到以下输出,正如我们所看到的,“洗涤和熨烫”重复了两次。反之亦然——当我先加“洗涤和折叠”,然后加“洗涤与熨烫”时,我会看到“洗涤与折叠”两次。

    在这里,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,
      })
    

    Screenshot

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

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

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

    1 回复  |  直到 1 年前
        1
  •  1
  •   Ivo    1 年前

    我不完全确定,但我认为你需要改变路线

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

    到线下

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

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