HiDayurie Dave
HiDayurie Dave

Reputation: 1807

Flutter list not update after do change the list

I have list data and try to change the value in position [0] to be value 5.

List<String> imagesVal = ['1', '2', '3', '4'];

Then I have change function

void changeImage(id, file, mediaID) {
    setState(() {
        imagesVal[0] = '5';
    })

    print(imagesVal);
});

The result is: ['5', '2', '3', '4']

Then I have save button

Future _save() async {
    print(imagesVal);
});

When tap the button, I got result still old value: ['1', '2', '3', '4']

My question, how to get the latest update value? On above example it should be get the value ['5', '2', '3', '4']

Upvotes: 0

Views: 645

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17812

You should have declared the list inside the build method of stateful widget by mistake. Please move it outside the build method.

Upvotes: 2

Related Questions