Dart Runes whereType()
Syntax & Examples
Runes.whereType() method
The `whereType` method in Dart returns a new lazy Iterable with all elements that have type T.
Syntax of Runes.whereType()
The syntax of Runes.whereType() method is:
Iterable<T> whereType<T>()
This whereType() method of Runes returns a new lazy Iterable with all elements that have type T
.
Return Type
Runes.whereType() returns value of type Iterable<T>
.
✐ Examples
1 Filtering integers from a mixed list
In this example,
- We create a list
mixedList
containing elements of various types. - We use the
whereType()
method with typeint
to filter integers. - We convert the resulting Iterable to a list and print it.
Dart Program
void main() {
List<dynamic> mixedList = [1, 'apple', true, 2.5, 'banana'];
Iterable<int> integers = mixedList.whereType<int>();
print(integers.toList());
}
Output
[1]
2 Filtering strings from a mixed list
In this example,
- We create a list
mixedList
containing elements of various types. - We use the
whereType()
method with typeString
to filter strings. - We convert the resulting Iterable to a list and print it.
Dart Program
void main() {
List<dynamic> mixedList = [1, 'apple', true, 2.5, 'banana'];
Iterable<String> strings = mixedList.whereType<String>();
print(strings.toList());
}
Output
[apple, banana]
3 Filtering integers from a mixed set
In this example,
- We create a set
mixedSet
containing elements of various types. - We use the
whereType()
method with typeint
to filter integers. - We convert the resulting Iterable to a list and print it.
Dart Program
void main() {
Set<dynamic> mixedSet = {1, 'apple', true};
Iterable<int> integers = mixedSet.whereType<int>();
print(integers.toList());
}
Output
[1]
Summary
In this Dart tutorial, we learned about whereType() method of Runes: the syntax and few working examples with output and detailed explanation for each example.