avoid_dynamic
Reports dynamic written as a type. Use Object? or a real type instead.
- Tier
- blocking
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”Every dynamic you write as:
- a type annotation: of a variable, field, parameter, return value, loop or pattern variable, or an extension type’s representation. Function and record types count too:
dynamic Function(dynamic)has two violations. - a type argument:
List<dynamic>,<dynamic>[],items.map<dynamic>(…),value as List<dynamic>.
It does not report:
Map<String, dynamic>, whileallow_json_mapsistrue(the default);- a value that is
dynamicwithout the word being written, such asvar later;, an untyped parameter or a rawList; Object?,Objectandvoid;- the word in a name, a string or a comment;
dynamicin a cast or a type test:value as dynamic,value is dynamic,case dynamic _,on dynamic catch (error);- a type alias for
dynamic(typedef Json = dynamic;) and its uses; dynamicas a type parameter bound (<T extends dynamic>) or as the type an extension is on;dynamicas an expression:type == dynamic.
An override is reported like any other declaration. Where the overridden member uses dynamic, you can write Object? instead.
dynamic turns type checking off for a value. Any member access on it compiles, even one that does not exist, and fails only at run time. It also spreads: whatever you read from a dynamic is dynamic too, so one annotation can leave a whole function unchecked.
Object? holds any value just as well, but keeps the check: you have to test the type with is or a pattern before you use the value.
The rule looks only at the written word. Values that become dynamic without it are covered by the analyzer’s strict modes (strict-raw-types, strict-inference, strict-casts) and by the SDK lint avoid_dynamic_calls.
Example
Section titled “Example”dynamic parsePort(dynamic settings) { return settings['port'];}int? parsePort(Map<String, Object?> settings) { final port = settings['port']; return port is int ? port : null;}Other ways to drop a dynamic:
- a value of unknown type:
Object?, narrowed withisor a pattern such asif (value case {'port': final int port}) { … }; - a collection of mixed values:
List<Object?>; - a callback whose argument you do not use:
void Function(Object?); - a type that depends on the caller: a type parameter,
T first<T>(List<T> values).
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
allow_json_maps |
true |
Allows Map<String, dynamic>, the type dart:convert gives a decoded JSON object. false reports it like any other dynamic. |
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: avoid_dynamic: allow_json_maps: falseallow_json_maps allows only a written Map<String, dynamic>: also nullable or nested (List<Map<String, dynamic>>), as a map literal (<String, dynamic>{'id': 1}) or as raw.cast<String, dynamic>(). These are still reported:
- other maps:
Map<int, dynamic>,Map<dynamic, dynamic>,Map<String?, dynamic>,HashMap<String, dynamic>; - the inner
dynamicofMap<String, List<dynamic>>; - a
Map<String, dynamic>built without writing the pair, such asmake<dynamic>()forMap<String, T> make<T>().
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The plugin interface declares this argument as dynamic; it is checked below.// ignore: assay/avoid_dynamicvoid onMessage(dynamic message) {// ignore_for_file: assay/avoid_dynamic silences it in a whole file, and // ignore: avoid_dynamic without the prefix works too. See Suppressions.