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

Time:12-15

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

CodePudding user response:

-> You Perform This Type Exaple Dart Pad EX:

 void main() {
      List<String>   OneData  = ['one','two','three'];  
      List<String>  TwoData = []; 
      TwoData.add('one'); 
      print(TwoData);
    }

CodePudding user response:

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

onTap: (){
    setState((){
        myList = [...state.myList, "newstring"];
    })
;}
  • Related