Flutter 正在从列表中删除项

Flutter 正在从列表中删除项,flutter,flutter-layout,Flutter,Flutter Layout,我有一个更新小部件元素的函数。它会更新除注释列表之外的所有列表,注释列表会添加新元素,但不会删除旧元素。下面是我回调函数的代码,该函数在索引处获取值,删除它,然后添加新值,然后导航回另一个页面: Function(int) onEditExercise = (int val) { setState( () { print(val); print("repExListBefore: ${widget.repExList}");

我有一个更新小部件元素的函数。它会更新除注释列表之外的所有列表,注释列表会添加新元素,但不会删除旧元素。下面是我回调函数的代码,该函数在索引处获取值,删除它,然后添加新值,然后导航回另一个页面:

  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中不起作用而在onDeleteExercise中不起作用吗
谢谢

记事本列表出现问题的原因是您在此行中使用了.remove()而不是.removeAt():

print("noteStringBefore: ${widget.noteList}"); 
widget.noteList.remove(val);   // this should be .removeAt(val)
我在这里做了一个飞镖垫,你可以玩它:


因此,作为“val”传递的是列表上的位置,而不是对象本身,对吗?顺便说一下,这是我见过的在Dart中声明方法的最复杂的方式。val是从当前单击的小部件发送的索引,很抱歉,忘了提及。我更新了这个问题,因为我意识到它在其他地方有效。谢谢这是一个完整的代码将非常有帮助的案例。。。或者至少是你的代码的精简版本,在那里你确实看到了问题。我看不出你的代码本身有什么问题。。。但是我看不到print语句(即print(val)、print(“noteStringBefore:${widget.noteList}”)等)的结果。如果您不能缩减代码,那么您至少应该使用与以前相同的“before/after”思维在代码中放入更多的print语句。如果没有这些,将很难帮助您排除故障。@WilliamTerrill我添加了更多的打印语句既然只有注释列表不好,您可以使用此代码打印日志吗?