62 lines
1.3 KiB
Plaintext
62 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In the interests of readability, code that can be simplified should be simplified. When you would like to filter elements of iterable by some type, it's more concise to use the method `whereType()`.
|
|
This approach improves readability and is less error-prone.
|
|
|
|
== How to fix it
|
|
|
|
Use `whereType` instead of `where((x) => x is Type)`.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,dart,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
void foo(Iterable<Vehicle> iterable)
|
|
{
|
|
iterable.where((x) => x is Car) // Noncompliant
|
|
.forEach((car) => print(car.model));
|
|
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,dart,diff-id=1,diff-type=compliant]
|
|
----
|
|
void foo(Iterable<Vehicle> iterable)
|
|
{
|
|
iterable.whereType<Car>()
|
|
.forEach((car) => print(car.model));
|
|
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Dart Docs - https://dart.dev/tools/linter-rules/prefer_iterable_whereType[Dart Linter rule - prefer_iterable_whereType]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
* Use 'whereType' to select elements of a given type.
|
|
|
|
=== Highlighting
|
|
|
|
The identifier of the `where` method call.
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
endif::env-github,rspecator-view[]
|