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
.
Parameter | Optional/Required | Description |
---|---|---|
index | required | the index at which to start overwriting elements in the list |
iterable | required | an iterable containing the elements to overwrite the existing elements in the list |
List.setAll() returns value of type void
.
In this example,
numbers
containing the integers [1, 2, 3, 4, 5]
.setAll()
method to overwrite elements starting from index 1 with the elements [6, 7, 8]
.[1, 6, 7, 8, 5]
.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
numbers.setAll(1, [6, 7, 8]);
}
[1, 6, 7, 8, 5]
In this example,
characters
containing the characters ['a', 'b', 'c', 'd']
.setAll()
method to overwrite elements starting from index 0 with the elements ['x', 'y']
.['x', 'y', 'c', 'd']
.void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
characters.setAll(0, ['x', 'y']);
}
[x, y, c, d]
In this example,
words
containing the strings ['apple', 'banana', 'cherry', 'date']
.setAll()
method to overwrite elements starting from index 1 with the element ['pear']
.['apple', 'pear', 'cherry', 'date']
.void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date'];
words.setAll(1, ['pear']);
}
[apple, pear, cherry, date]
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.