Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding elements to the List inside setState() method, not saving element Flutter

I'm adding another list items to new List and performing this code inside the setState(). But whenever I call this class in which the new list is initialized, list's previous entry gets disappear.. dont know why this happening. Also im new to flutter. thanks in advance.

List<String> myList = [];

inside build(stateful widget),

onTap: (){
setState((){
myList.add("newstring");
});}
like image 903
NabsDev Avatar asked Sep 02 '25 10:09

NabsDev


1 Answers

you should copy objects in setState, in your case it should be

onTap: () {
    setState((){
        myList = [...state.myList, "newstring"];
    });
}
like image 115
no_fate Avatar answered Sep 04 '25 08:09

no_fate