Skip to main content

avoid-type-casts

added in: 1.22.0
⚙️
Pro+

Warns about any usages of the as operator.

⚙️ Config

Set only-dynamic (default is false) to highlight only type casts for variables with the dynamic type.

dart_code_metrics:
...
rules:
- avoid-type-casts:
only-dynamic: false

Example

❌ Bad:

void fn() {
dynamic a = 3;
final s = a as String; // LINT

int b = 2;

final s = b as String; // LINT
if (b case String() as Object) {} // LINT
}

✅ Good:

void fn(List<String> values) {
dynamic a = 3;
if (a is String) {
...
}
}