从列表中删除项目Flutter [英] Removing item from list Flutter

查看:576
本文介绍了从列表中删除项目Flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a function which updates elements of a widget. It updates all lists apart from the note list which adds the new element but does not delete the old one. Here is the code where I callback a function which takes the value at index, removes it and then adds the new value and then navigates back to a different page:

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

这是更改所有列表的第一个小部件的值后打印出来的结果:

This is the result from the prints after changing the first widget's values for all lists:

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]

当我在另一个页面中完全删除小部件时,相同的代码也起作用:

The same code works when i remove a widget entirely in another page:

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

Any ideas why it is not working in onDeleteExercise but not on onEditExercise Thank you

推荐答案

noteList出现问题的原因是您在此行使用的是.remove()而不是.removeAt():

The reason you're having problems with noteList is that you're using .remove() instead of .removeAt() at this line:

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

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

I made a dartpad here so you can play with it:

https://dartpad.dev/81889b830d6a37873d449c8d143d0f71

这篇关于从列表中删除项目Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆