flutterNoob
flutterNoob

Reputation: 67

Removing item from list Flutter

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

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

Upvotes: 0

Views: 269

Answers (1)

William Terrill
William Terrill

Reputation: 3744

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

Upvotes: 1

Related Questions