What is List replaceRange() in Dart?

The replaceRange() function

To update List elements, you can use the replaceRange() function from the dart:core library’s List class.

The replaceRange() method replaces the value of the element(s) within the provided range.

Syntax

List.replaceRange(int start_index, int end_index, Iterable <items>)

Parameters

  • start_index: An integer value that represents the index position at which the replacement should begin.

  • end_index: An integer value that represents the index position before which the replacement should terminate.

  • <items>: An iterable object with the modified values.

Return value

This method does not return a value.

Example

The following example demonstrates how to use the replaceRange() method in dart. Click on the “Run” button to execute the code.

void main() {
// Creating List
List<String> fruits = ["Orange","Apple", "Mango", "Banana", "Avocado"];
// display result
print('The list item before replacing: ${fruits}');
// replace items using replaceRange()
fruits.replaceRange(0,3,["Pineapple", "Pawpaw", "Lemon",]);
// display updated result
print('The new list after replacing the items between the range [0-3] is ${fruits}');
}

Free Resources