The syntax of List.whereType() method is:
Iterable<T> whereType<T>()
This whereType() method of List creates a new lazy Iterable
with all elements that have type T
.
List.whereType() returns value of type Iterable<T>
.
In this example,
mixedList
containing elements of various types.whereType()
method on mixedList
, specifying the type int
as the filter condition.result
contains only the elements that are of type int
.void main() {
List<Object> mixedList = [1, 'two', 3.0, true];
var result = mixedList.whereType<int>();
print(result); // Output: [1]
}
(1, 3)
In this example,
mixedList
containing elements of various types.whereType()
method on mixedList
, specifying the type String
as the filter condition.result
contains only the elements that are of type String
.void main() {
List<Object> mixedList = [1, 'two', 3.0, true];
var result = mixedList.whereType<String>();
print(result); // Output: [two]
}
[two]
In this example,
mixedList
containing elements of various types.whereType()
method on mixedList
, specifying the type double
as the filter condition.result
contains only the elements that are of type double
.void main() {
List<Object> mixedList = [1, 'two', 3.0, true];
var result = mixedList.whereType<double>();
print(result); // Output: [3.0]
}
(1, 3)
In this Dart tutorial, we learned about whereType() method of List: the syntax and few working examples with output and detailed explanation for each example.