从列表Flutter中删除项目

问题描述 投票:0回答:1

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

  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上运行谢谢

flutter flutter-layout
1个回答
0
投票

您在noteList上遇到问题的原因是,您在此行使用.remove()而不是.removeAt():

print("noteStringBefore: ${widget.noteList}"); 
widget.noteList.remove(val);   // this should be .removeAt(val)

我在这里做了一个飞镖,所以您可以玩它:

https://dartpad.dev/81889b830d6a37873d449c8d143d0f71

© www.soinside.com 2019 - 2024. All rights reserved.