Here is what I'm trying to do.
Here is my ListItems class:
class ListItems {
String? title;
String? text;
DateTime? selectedDayTime;
ListItems(
{required this.title, required this.text, required this.selectedDayTime});
}
In my AddTask class, I'm trying to add some items to my list. Here is my code:
ElevatedButton(
onPressed: () {
setState(() {
itemList.add(
ListItems(
title: _titleController.text,
text: _textController.text,
selectedDayTime: _selectedDay,
),
);
Navigator.pop(context);
});
print(itemList.length);
},
child: Text("Add Task"),
),
itemList is a List list. However, everytime I try to add new item to my itemList, itemList.length never changes. I dont know why this happening so I need help.
CodePudding user response:
Swap Navigator with print line.
ElevatedButton(
onPressed: () {
setState(() {
itemList.add(
ListItems(
title: _titleController.text,
text: _textController.text,
selectedDayTime: _selectedDay,
),
);
print(itemList.length);
//Navigator.pop(context); keep it here also
});
Navigator.pop(context);
},
child: Text("Add Task"),
),
CodePudding user response:
Does it make any difference if you include your print statement inside the setState
? I don't see any reason for your code not to work.
You could also try to type your itemList
to be of type List<ListItems>
, if you haven't already. I don't think it's causing any issues, but it can't hurt to be thorough.