- Dart Programming Tutorial
- Dart Programming - Home
- Dart Programming - Overview
- Dart Programming - Environment
- Dart Programming - Syntax
- Dart Programming - Data Types
- Dart Programming - Variables
- Dart Programming - Operators
- Dart Programming - Loops
- Dart Programming - Decision Making
- Dart Programming - Numbers
- Dart Programming - String
- Dart Programming - Boolean
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Map
- Dart Programming - Symbol
- Dart Programming - Runes
- Dart Programming - Enumeration
- Dart Programming - Functions
- Dart Programming - Interfaces
- Dart Programming - Classes
- Dart Programming - Object
- Dart Programming - Collection
- Dart Programming - Generics
- Dart Programming - Packages
- Dart Programming - Exceptions
- Dart Programming - Debugging
- Dart Programming - Typedef
- Dart Programming - Libraries
- Dart Programming - Async
- Dart Programming - Concurrency
- Dart Programming - Unit Testing
- Dart Programming - HTML DOM
- Dart Programming Useful Resources
- Dart Programming - Quick Guide
- Dart Programming - Resources
- Dart Programming - Discussion
Dart Programming - Updating a list
Updating the index
Dart allows modifying the value of an item in a List. In other words, one can re-write the value of list item. The following example illustrates the same −
void main() { List l = [1, 2, 3,]; l[0] = 123; print (l); }
The above example updates the value of the List item with index 0. The output of the code will be −
[123, 2, 3]
Using the List.replaceRange() function
The List class from the dart:core library provides the replaceRange() function to modify List items. This function replaces the value of the elements within the specified range.
The syntax for using List.replaceRange() function is as given below −
List.replaceRange(int start_index,int end_index,Iterable <items>)
Where,
Start_index − an integer representing the index position to start replacing.
End_index − an integer representing the index position to stop replacing.
<items> − an iterable object that represents the updated values.
The following example illustrates the same −
Live Demovoid main() { List l = [1, 2, 3,4,5,6,7,8,9]; print('The value of list before replacing ${l}'); l.replaceRange(0,3,[11,23,24]); print('The value of list after replacing the items between the range [0-3] is ${l}'); }
It should produce the following output −
The value of list before replacing [1, 2, 3, 4, 5, 6, 7, 8, 9] The value of list after replacing the items between the range [0-3] is [11, 23, 24, 4, 5, 6, 7, 8, 9]