In this guide, we will discuss Updating a list in Dart Programming Language.
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 −
void 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]
Next Topic : Click Here
Pingback: Inserting Elements into a List | Adglob Infosystem Pvt Ltd
Pingback: Dart Programming - Lists (Basic Operations) | Adglob Infosystem Pvt Ltd