List.replaceRange() function


Advertisements

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] 
dart_programming_lists_basic_operations.htm
Advertisements