【问题标题】:Removing item from list Flutter从列表中删除项目 Flutter
【发布时间】:2020-03-31 12:32:37
【问题描述】:

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

  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 谢谢

【问题讨论】:

  • 那么你作为“val”传递的是列表中的位置,而不是对象本身,对吧?顺便说一句,这是我见过的在 Dart 中声明方法的最复杂的方式。
  • val 是从当前点击的小部件发送的索引,抱歉忘了提及。我已经更新了这个问题,因为我意识到它可以在其他地方工作。谢谢
  • 这是完整代码非常有用的情况之一......或者至少是您实际看到问题的代码的精简版本。我看不出您的代码本身有什么问题......但我看不到打印语句的结果(即 print(val)、print("noteStringBefore: ${widget.noteList}") 等)如果你不能削减你的代码,那么你至少应该在你的代码中使用与以前相同的“之前/之后”的心态在代码中添加更多的打印语句。否则,将很难帮助您进行故障排除。
  • @WilliamTerrill 我添加了更多打印声明
  • 既然只有noteList不好,可以用这段代码打印日志吗?

标签: flutter flutter-layout


【解决方案1】:

您在使用 noteList 时遇到问题的原因是您在这一行使用 .remove() 而不是 .removeAt():

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

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

https://dartpad.dev/81889b830d6a37873d449c8d143d0f71

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2022-12-20
    • 2019-08-16
    • 2021-01-19
    • 2016-05-08
    • 2011-03-18
    相关资源
    最近更新 更多