The syntax of List.fillRange() method is:
void fillRange(int start, int end, [E? fillValue])
This fillRange() method of List overwrites a range of elements with fillValue
.
Parameter | Optional/Required | Description |
---|---|---|
start | required | the start index of the range to fill |
end | required | the end index of the range to fill |
fillValue | optional | the value used to fill the range, defaults to null if not provided |
List.fillRange() returns value of type void
.
In this example,
numbers
containing the numbers 1, 2, 3, 4, 5
.fillRange()
method to fill the range from index 1
to 3
with zeros.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
numbers.fillRange(1, 3, 0);
print('Filled range with zeros: $numbers'); // Output: Filled range with zeros: [1, 0, 0, 4, 5]
}
Filled range with zeros: [1, 0, 0, 4, 5]
In this example,
characters
containing the characters 'a', 'b', 'c', 'd', 'e'
.fillRange()
method to fill the range from index 2
to 4
with the character 'x'
.void main() {
List<String> characters = ['a', 'b', 'c', 'd', 'e'];
characters.fillRange(2, 4, 'x');
print('Filled range with \'x\': $characters'); // Output: Filled range with 'x': [a, b, x, x, e]
}
Filled range with 'x': [a, b, x, x, e]
In this example,
words
containing the strings 'apple', 'banana', 'cherry', 'date', 'fig'
.fillRange()
method to fill the range from index 1
to 4
with the string 'orange'
.void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date', 'fig'];
words.fillRange(1, 4, 'orange');
print('Filled range with \'orange\': $words'); // Output: Filled range with 'orange': [apple, orange, orange, orange, fig]
}
Filled range with 'orange': [apple, orange, orange, orange, fig]
In this Dart tutorial, we learned about fillRange() method of List: the syntax and few working examples with output and detailed explanation for each example.