Dart List setAll()
Syntax & Examples


Syntax of List.setAll()

The syntax of List.setAll() method is:

 void setAll(int index, Iterable<E> iterable) 

This setAll() method of List overwrites elements with the objects of iterable.

Parameters

ParameterOptional/RequiredDescription
indexrequiredthe index at which to start overwriting elements in the list
iterablerequiredan iterable containing the elements to overwrite the existing elements in the list

Return Type

List.setAll() returns value of type void.



✐ Examples

1 Overwrite elements from index 1 in the list of numbers

In this example,

  1. We create a list named numbers containing the integers [1, 2, 3, 4, 5].
  2. We then use the setAll() method to overwrite elements starting from index 1 with the elements [6, 7, 8].
  3. As a result, the list becomes [1, 6, 7, 8, 5].
  4. We print the modified list to standard output.

Dart Program

void main() {
  List&lt;int&gt; numbers = [1, 2, 3, 4, 5];
  numbers.setAll(1, [6, 7, 8]);
}

Output

[1, 6, 7, 8, 5]

2 Overwrite elements from index 0 in the list of characters

In this example,

  1. We create a list named characters containing the characters ['a', 'b', 'c', 'd'].
  2. We then use the setAll() method to overwrite elements starting from index 0 with the elements ['x', 'y'].
  3. As a result, the list becomes ['x', 'y', 'c', 'd'].
  4. We print the modified list to standard output.

Dart Program

void main() {
  List&lt;String&gt; characters = ['a', 'b', 'c', 'd'];
  characters.setAll(0, ['x', 'y']);
}

Output

[x, y, c, d]

3 Overwrite elements from index 1 in the list of words

In this example,

  1. We create a list named words containing the strings ['apple', 'banana', 'cherry', 'date'].
  2. We then use the setAll() method to overwrite elements starting from index 1 with the element ['pear'].
  3. As a result, the list becomes ['apple', 'pear', 'cherry', 'date'].
  4. We print the modified list to standard output.

Dart Program

void main() {
  List&lt;String&gt; words = ['apple', 'banana', 'cherry', 'date'];
  words.setAll(1, ['pear']);
}

Output

[apple, pear, cherry, date]

Summary

In this Dart tutorial, we learned about setAll() method of List: the syntax and few working examples with output and detailed explanation for each example.