avoid-return-types-on-setters

Same as avoid_return_types_on_setters in Dart lints

Group
Style
Path
lint/style/avoid-return-types-on-setters
Default
On (recommended)
Origin
Dart lints
Rule
130 of 512
recommended

Flags setters declared with an explicit return type.

A setter never returns a value, so annotating one with a return type — even void — is redundant and can mislead readers into thinking the result is usable. Drop the return type and write set foo(int v). Both top-level setters and setters declared in classes, mixins, enums, and extensions are checked.

Invalid

example.dartdart
// Setters never return a value, so any return type is redundant.
class A {
  int _v = 0;
  void set value(int v) { _v = v; }
  int _w = 0;
  int set width(int v) { _w = v; }
}

class B {
  String _n = '';
  String set name(String v) { _n = v; }
  static void set flag(bool v) {}
  external void set ext(int v);
}

mixin M {
  void set data(int v) {}
}
Avoid return types on setters.
3 int _v = 0;
4 void set value(int v) { _v = v; }
Avoid return types on setters.
5 int _w = 0;
6 int set width(int v) { _w = v; }
Avoid return types on setters.
10 String _n = '';
11 String set name(String v) { _n = v; }
Avoid return types on setters.
11 String set name(String v) { _n = v; }
12 static void set flag(bool v) {}
Avoid return types on setters.
12 static void set flag(bool v) {}
13 external void set ext(int v);
Avoid return types on setters.
16mixin M {
17 void set data(int v) {}

Valid

example.dartdart
// Setters declared without a return type — the correct form.
class A {
  int _v = 0;
  set value(int v) { _v = v; }
  int _w = 0;
  set width(int v) => _w = v;
  static set flag(bool v) {}
  external set ext(int v);
  // Comments between modifiers are not return types.
  static /* cached */ set cached(int v) {}
  set /* note */ noted(int v) {}
  static // trailing note
      set commented(int v) {}
}

mixin M {
  set data(int v) {}
}

class B {
  int getValue() => 1;
  void doThing() {}
}

Configure avoid-return-types-on-setters

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-return-types-on-setters": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-return-types-on-setters: <reason>