0

Hi I have a list of Daytile objects and I want to remove them based on an AlertDialog that asks the user to input the name of the dayTile they want to remove. However, even when inputting the name exactly as it is, the element is never removed from the list.

Removal Logic

TextFormField(controller: titleController, inputFormatters: [
                LengthLimitingTextInputFormatter(14),
              ]),

...

TextButton(
                        onPressed: () async {
                          setState(() {
                            dayTiles.remove(
                                DayTile(title: titleController.text));
                            // print(dayTiles);
                          });
                          saveData();
                          Navigator.pop(context);
                        },
                        child: const Text("Remove")),

Thank You!

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

1 Answers1

1

You are creating new instance, what do you need is to find item index on this case.

final itemIndex = dayTiles.indexWhere((element) => element.title == titleController.text);
  if(itemIndex>-1)
      dayTiles.removeAt(itemIndex));

Find more about What is the true meaning of pass-by-reference in modern languages like Dart?

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56