【发布时间】: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)]
我想要实现的是,如果地图key 在answerTable.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!!!
【问题讨论】: