Dart Runes take()
Syntax & Examples
Runes.take() method
The `take` method in Dart returns a lazy iterable of the count first elements of this iterable.
Syntax of Runes.take()
The syntax of Runes.take() method is:
Iterable<int> take(int count)
This take() method of Runes returns a lazy iterable of the count
first elements of this iterable.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
count | required | The number of elements to take from the beginning of the iterable. |
Return Type
Runes.take() returns value of type Iterable<int>
.
✐ Examples
1 Taking elements from a list of numbers
In this example,
- We create a list
numbers
containing integers. - We use the
take()
method to take the first 3 elements. - We convert the resulting Iterable to a list and print it.
Dart Program
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> taken = numbers.take(3);
print(taken.toList());
}
Output
[1, 2, 3]
2 Taking elements from a list of strings
In this example,
- We create a list
words
containing strings. - We use the
take()
method to take the first 2 elements. - We convert the resulting Iterable to a list and print it.
Dart Program
void main() {
List<String> words = ['apple', 'banana', 'cherry'];
Iterable<String> taken = words.take(2);
print(taken.toList());
}
Output
[apple, banana]
3 Taking elements from a set of numbers
In this example,
- We create a set
uniqueNumbers
containing integers. - We use the
take()
method to take the first element. - We convert the resulting Iterable to a list and print it.
Dart Program
void main() {
Set<int> uniqueNumbers = {1, 2, 3};
Iterable<int> taken = uniqueNumbers.take(1);
print(taken.toList());
}
Output
[1]
Summary
In this Dart tutorial, we learned about take() method of Runes: the syntax and few working examples with output and detailed explanation for each example.