AvoidDynamicRule class

Rule that discourages use of 'dynamic' type.

The 'dynamic' type bypasses static type checking and can lead to runtime errors that could have been caught at compile time.

Known Limitations

This rule only detects explicit dynamic type annotations. It uses syntactic analysis without resolved type information, so it cannot detect implicit dynamic from type inference failures.

Detected (Explicit Dynamic)

dynamic x;                    // Detected
Map<String, dynamic> map;     // Detected (nested)
void fn(dynamic param) {}     // Detected
x as dynamic;                 // Detected

NOT Detected (Implicit Dynamic)

var x = json['key'];          // NOT detected (inferred dynamic)
final data = response.body;   // NOT detected if body is dynamic
list.map((e) => e.name);      // NOT detected if e is inferred dynamic

Why This Limitation Exists

Detecting implicit dynamic requires resolved type information from the Dart analyzer, which adds significant complexity and performance overhead. This rule prioritizes speed and simplicity for the common case of catching explicit dynamic annotations that developers consciously write.

Complementary Analysis

For comprehensive dynamic detection, use Dart's strict analysis options:

analyzer:
  language:
    strict-casts: true
    strict-inference: true
    strict-raw-types: true
Inheritance

Constructors

AvoidDynamicRule()

Properties

category RuleCategory
Category this rule belongs to.
no setteroverride
defaultSeverity RuleSeverity
Default severity for violations of this rule.
no setteroverride
description String
Human-readable description of what this rule checks.
no setteroverride
documentationUrl String?
Documentation URL for this rule.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
id String
Unique identifier for this rule (e.g., 'avoid-dynamic').
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

check(CompilationUnit unit, {LineInfo? lineInfo}) List<Violation>
Check the compilation unit for violations.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited