Tony
Tony

Reputation: 2748

Compare hashMap value with List item

I have a map, named finalMap;

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

It holds question id and answer.

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

Also I have a answerTable list, named answerTable (List)

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

What I trying to achieve is if the map key is exits in answerTable.sectionItemId, it call edit api, otherwise call 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
          }
        }
      }
    }
  }

But I get a weird result. At first I can call the create api when answerTable length is empty. But when I go to edit it, it will call create api after edit api called..What the issue here?

Output

{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!!!

Upvotes: 0

Views: 274

Answers (1)

Hemanth S
Hemanth S

Reputation: 688

Something like this you can do it's not 100% solution but you can work around it. I have made mock you can use release classes and variables

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

Upvotes: 1

Related Questions