我有一个更新小部件元素的功能。它会更新除注释列表之外的所有列表,注释列表会添加新元素,但不会删除旧元素。这是我在其中回调一个函数的代码,该函数在索引处获取值,将其删除,然后添加新值,然后导航回到另一个页面:
Function(int) onEditExercise = (int val) {
setState(
() {
print(val);
print("repExListBefore: ${widget.repExList}");
widget.repExList.removeAt(val);
print("freqListBefore: ${widget.freqList}");
widget.freqList.removeAt(val);
print("holdForListBefore: ${widget.holdForList}");
widget.holdForList.removeAt(val);
print("noteStringBefore: ${widget.noteList}");
widget.noteList.remove(val);
widget.repExList
.insert(val, _currentRepExSelected ?? widget.repeatEx);
widget.holdForList
.insert(val, _currentHoldForSelected ?? widget.holdF);
widget.freqList.insert(val, _currentFreqSelected ?? widget.freq);
widget.noteList.insert(val, _notes.text);
print("repExListAfter: ${widget.repExList}");
print("freqListAfter: ${widget.freqList}");
print("holdForListAfter: ${widget.holdForList}");
print("noteStringAfter: ${widget.noteList}");
Navigator.of(context)
.push(MaterialPageRoute(builder: (BuildContext context) {
return EditScheduleScreen(
repExList: widget.repExList,
holdForList: widget.holdForList,
freqList: widget.freqList,
noteList: widget.noteList,
imageURLList: widget.imageURLList,
videoURLList: widget.videoURLList,
count: widget.count,
therapistName: widget.therapistName,
name: widget.name,
);
}));
},
);
};
这是更改所有列表的第一个小部件的值后打印出来的结果:
flutter: 0
flutter: repExListBefore: [1 time, 1 time, 1 time]
flutter: freqListBefore: [Once a day, Once a day, Once a day]
flutter: holdForListBefore: [10 seconds, 10 seconds, 10 seconds]
flutter: noteStringBefore: [a, b, c]
flutter: repExListAfter: [2 times, 1 time, 1 time]
flutter: freqListAfter: [Twice a day, Once a day, Once a day]
flutter: holdForListAfter: [20 seconds, 10 seconds, 10 seconds]
flutter: noteStringAfter: [change ‘a’, a, b, c]
当我在另一个页面中完全删除小部件时,相同的代码也起作用:
Function(int) onDeleteExercise = (int val) {
setState(
() {
print(val);
widget.repExList.removeAt(val);
widget.freqList.removeAt(val);
widget.noteList.removeAt(val);
widget.holdForList.removeAt(val);
widget.imageURLList.removeAt(val);
widget.videoURLList.removeAt(val);
children.removeAt(val);
widget.count--;
},
);
};
任何想法为什么它不能在onDeleteExercise上运行但不能在onEditExercise上运行 谢谢
答案 0 :(得分:1)
noteList出现问题的原因是您在此行使用了.remove()而不是.removeAt():
print("noteStringBefore: ${widget.noteList}");
widget.noteList.remove(val); // this should be .removeAt(val)
我在这里制作了一个飞镖,所以您可以玩它: