【问题标题】:Compare hashMap value with List item将 hashMap 值与 List 项进行比较
【发布时间】:2021-12-13 00:04:57
【问题描述】:

我有一张地图,名为finalMap

var finalMap = Map<String, String>();

它包含问题 ID 和答案。

The output is {17: W, 19: N, 83: yes}

我还有一个answerTable 列表,名为 answerTable (List)

[ChecklistAnswerItem(id: 142, sectionItemId: 17, checklistsectionItem: Instance of 'ChecklistAnswerSectionItem', inputAnswer: W)]

我想要实现的是,如果地图keyanswerTable.sectionItemId 中退出,则调用edit api,否则调用create api。

 Future editChecklistAnswer(
      String id, String checklistAnswerId, Map<String, String> textMap) async {

    List<ChecklistAnswerItem> answerTable =
        await _repository.selectChecklistAnswerItemsList();

           ..
    print(finalMap);
    print(answerTable);

    for (final entry in finalMap.entries) {
      if (answerTable.isEmpty) {
        print("submit" + entry.key.toString());
       // I have my create api here
      } else {
        for (var i in answerTable) {
          if (entry.key.toString() == i.sectionItemId.toString()) {
            print("edit " + entry.key.toString());
           // I have edit api here
          } else {
            print("create " + entry.key.toString());
           // I have create api here
          }
        }
      }
    }
  }

但我得到了一个奇怪的结果。起初我可以在answerTable长度为空时调用create api。但是当我去编辑它时,它会在调用edit api后调用create api..这里有什么问题?

输出

{17: T, 16: v}
[]
submit 17
submit 16
{17:  T, 16: 123}
[ChecklistAnswerItem(id: 388, sectionItemId: 17, checklistsectionItem: Instance of 'ChecklistAnswerSectionItem', inputAnswer: W), ChecklistAnswerItem(id: 389, sectionItemId: 16, checklistsectionItem: Instance of 'ChecklistAnswerSectionItem', inputAnswer: 123)]
edit 17
create 17   // it suppost not to call create api!!!
create 16  // it suppost not to call create api!!!

【问题讨论】:

    标签: list flutter dart hashmap


    【解决方案1】:

    这样的事情你可以做到,它不是 100% 的解决方案,但你可以解决它。我做了模拟,你可以使用发布类和变量

        void checkForAction() {
          // Check list but sending ID you want
          editChecklistAnswer(finalMap.keys.first);
        }
    
        Future editChecklistAnswer(int checklistAnswerId) async {
          List<ChecklistAnswerItem> answerTable = answerTableList;
          if (answerTable.isEmpty) {
            //TODO:Submit
          }
          bool isAnswerExist = answerTable
              .where((element) => element.sectionItemId == checklistAnswerId)
              .isNotEmpty;
          if (isAnswerExist) {
            //TODO: Call Edit API
          } else {
            //TODO: Call crease API
          }
        }
    
        final Map<int, String> finalMap = {1: 'W', 2: 'N', 3: 'Yes'};
    
        final answerTableList = [
          ChecklistAnswerItem(1),
          ChecklistAnswerItem(2),
          ChecklistAnswerItem(3),
        ];
    
        class ChecklistAnswerItem {
          final int sectionItemId;
    
          ChecklistAnswerItem(this.sectionItemId);
        }
    

    【讨论】:

    • 感谢您的建议...如果问题 id 存在于answerTable 中,我想将ChecklistAnswerItem.id 传递给创建 api ..我怎样才能获得ChecklistAnswerItem.id 基于你的答案?
    • checklistAnswerId 被传递给函数,如果 isAnswerExist 为真,则传递给创建 API 调用