avoid-annotating-with-dynamic

Same as avoid_annotating_with_dynamic in Dart lints

Group
Style
Path
lint/style/avoid-annotating-with-dynamic
Default
Off, opt in
Origin
Dart lints
Rule
14 of 512

Flags parameters explicitly annotated with dynamic.

An unannotated parameter is already dynamic (or inferred from an override), so writing dynamic adds noise. This covers regular, field (this.x), and super (super.x) formals, function-typed formals whose return type is dynamic (dynamic f()), setter parameters, and the parameters of function types. dynamic must resolve through dart:core: a file that imports dart:core only with a prefix sees core.dynamic, not a bare dynamic.

void log(dynamic value) {} // flagged
void log(value) {}         // ok

Invalid

example.dartdart
class Holder {
  dynamic value;
  Holder(dynamic this.value);
  void set(dynamic v) {}
  set current(dynamic v) {}
}

class Child extends Holder {
  Child(dynamic super.value);
}

void log(dynamic message) {}

void named({dynamic extra}) {}

// A function-typed formal returning `dynamic` is annotated with `dynamic` too.
void callback(dynamic f()) {}

typedef Callback = void Function(dynamic event);

void main() {
  final f = (dynamic x) => x;
  print(f);
}
Remove the unnecessary `dynamic` type annotation.
2 dynamic value;
3 Holder(dynamic this.value);
Remove the unnecessary `dynamic` type annotation.
3 Holder(dynamic this.value);
4 void set(dynamic v) {}
Remove the unnecessary `dynamic` type annotation.
4 void set(dynamic v) {}
5 set current(dynamic v) {}
Remove the unnecessary `dynamic` type annotation.
8class Child extends Holder {
9 Child(dynamic super.value);
Remove the unnecessary `dynamic` type annotation.
11
12void log(dynamic message) {}
Remove the unnecessary `dynamic` type annotation.
13
14void named({dynamic extra}) {}
Remove the unnecessary `dynamic` type annotation.
16// A function-typed formal returning `dynamic` is annotated with `dynamic` too.
17void callback(dynamic f()) {}
Remove the unnecessary `dynamic` type annotation.
18
19typedef Callback = void Function(dynamic event);
Remove the unnecessary `dynamic` type annotation.
21void main() {
22 final f = (dynamic x) => x;

Valid

example.dartdart
class Holder {
  dynamic value;
  Holder(this.value);
  void set(v) {}
  set current(v) {}
}

void log(Object? message) {}

void untyped(message) {}

dynamic decode(String json) => json;

typedef Legacy(event);

typedef Callback = void Function(Object? event);

void main() {
  final f = (x) => x;
  final List<dynamic> list = [];
  dynamic local = 1;
  print([f, list, local]);
}

Configure avoid-annotating-with-dynamic

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-annotating-with-dynamic": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-annotating-with-dynamic: <reason>