Skip to main content

no_dynamic_casts

Learn about the no_dynamic_casts linter rule.

Unreleased
Released in Dart 3.13

Avoid implicit casts from dynamic.

Details

#

DO avoid implicit casts from dynamic.

Assigning a dynamic-typed expression to a non-dynamic, non-Object? target is a form of implicit casting. It is better to make such a cast explicit, so that it is visible to developers.

BAD:

dart
void f(dynamic x) {
  int y = x;
}

GOOD:

dart
void f(dynamic x) {
  int y = x as int;
}

Enable

#

To enable the no_dynamic_casts rule, add no_dynamic_casts under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - no_dynamic_casts

If you're instead using the YAML map syntax to configure linter rules, add no_dynamic_casts: true under linter > rules:

analysis_options.yaml
yaml
linter:
  rules:
    no_dynamic_casts: true