Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator
list
index

How to update a list item using its index in Dart

Maria Elijah

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

RELATED TAGS

dart
communitycreator
list
index
RELATED COURSES

View all Courses

Keep Exploring