How to update a list item using its index in Dart

Overview

To replace the value of a single item with a different value, we can use that item’s index and assign it a new value.

Syntax

list_name[index] = newValue;

list_name is the name of the list.

Code

Let’s assume that we have a list that is named numbers.

List numbers = [20, 30, 40, 50, 60, 70];

We can use the code given below to change the value from 20 to 40 by using the index of 20.

void main() {
//Creating list
List numbers = [20, 30, 40, 50, 60, 70];
print('The list items before updating: ${numbers}');
numbers[0] = 40 ; // The index of 20 is 0
//display result
print('The list items after updating: ${numbers}');
}

Free Resources