Skip to content

avoid_dynamic

Reports dynamic written as a type. Use Object? or a real type instead.

Tier
blocking
Scope
Dart
Fix
none

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>, while allow_json_maps is true (the default);
  • a value that is dynamic without the word being written, such as var later;, an untyped parameter or a raw List;
  • Object?, Object and void;
  • the word in a name, a string or a comment;
  • dynamic in 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;
  • dynamic as a type parameter bound (<T extends dynamic>) or as the type an extension is on;
  • dynamic as 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.

Violating
dynamic parsePort(dynamic settings) {
return settings['port'];
}
Corrected
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 with is or a pattern such as if (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).

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: false

allow_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 dynamic of Map<String, List<dynamic>>;
  • a Map<String, dynamic> built without writing the pair, such as make<dynamic>() for Map<String, T> make<T>().

Write why on the line directly above the suppression:

// The plugin interface declares this argument as dynamic; it is checked below.
// ignore: assay/avoid_dynamic
void 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.