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
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 |
Return Type
List.setAll() returns value of type void
.
✐ Examples
1 Overwrite elements from index 1 in the list of numbers
In this example,
- We create a list named
numbers
containing the integers[1, 2, 3, 4, 5]
. - We then use the
setAll()
method to overwrite elements starting from index 1 with the elements[6, 7, 8]
. - As a result, the list becomes
[1, 6, 7, 8, 5]
. - We print the modified list to standard output.
Dart Program
void main() {
List<int> 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,
- We create a list named
characters
containing the characters['a', 'b', 'c', 'd']
. - We then use the
setAll()
method to overwrite elements starting from index 0 with the elements['x', 'y']
. - As a result, the list becomes
['x', 'y', 'c', 'd']
. - We print the modified list to standard output.
Dart Program
void main() {
List<String> 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,
- We create a list named
words
containing the strings['apple', 'banana', 'cherry', 'date']
. - We then use the
setAll()
method to overwrite elements starting from index 1 with the element['pear']
. - As a result, the list becomes
['apple', 'pear', 'cherry', 'date']
. - We print the modified list to standard output.
Dart Program
void main() {
List<String> 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.