empty-constructor-bodies

Same as empty_constructor_bodies in Dart lints

Group
Style
Path
lint/style/empty-constructor-bodies
Default
On (recommended)
Origin
Dart lints
Rule
230 of 512
recommended

Flags constructors with an empty block body {}.

An empty {} is indistinguishable from a body someone forgot to fill in, whereas a bare ; states plainly that the constructor does no extra work. Prefer Foo(); over Foo() {}. Only a block body containing no statements is reported; a constructor whose body does something is left alone.

Invalid

example.dartdart
class A {
  A() {}
}

class B {
  final int x;
  B(this.x) {}
}

class C {
  C.named() {}
}

class D {
  int y = 0;
  D() : y = 1 {}
}

class E {
  E.first() {}
  E.second() {}
}
Use `;` instead of `{}` for this empty constructor body.
1class A {
2 A() {}
Use `;` instead of `{}` for this empty constructor body.
6 final int x;
7 B(this.x) {}
Use `;` instead of `{}` for this empty constructor body.
10class C {
11 C.named() {}
Use `;` instead of `{}` for this empty constructor body.
15 int y = 0;
16 D() : y = 1 {}
Use `;` instead of `{}` for this empty constructor body.
19class E {
20 E.first() {}
Use `;` instead of `{}` for this empty constructor body.
20 E.first() {}
21 E.second() {}

Valid

example.dartdart
class A {
  A();
}

class B {
  final int x;
  B(this.x);
}

class C {
  C.named();
}

class D {
  int y = 0;
  D() : y = 1;
}

class E {
  E() {
    print('init');
  }
}

class F {
  final int z;
  F(this.z) {
    print(z);
  }
}

Configure empty-constructor-bodies

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "empty-constructor-bodies": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/empty-constructor-bodies: <reason>
  • enum-constants-ordering

    Flags enum constants whose declared names violate the configured order.

  • eol-at-end-of-fileeol_at_end_of_filerecommended

    Flags a non-empty file that does not end with exactly one line break.

  • extend-equatableequatable

    Require Equatable semantics for configured class/file name patterns.

  • file-namesfile_namesrecommended

    Enforces lowercase_with_underscores for source file names.

  • flutter-style-todosflutter_style_todos

    Flags TODO comments that do not follow the Flutter style // TODO(username): message, https://URL-to-issue.

  • format-commentformat-comment

    Flags comments that are not formatted as complete sentences.

  • implicit-call-tearoffsimplicit_call_tearoffs

    Explicitly tear off callable objects' call methods.

  • initializers-ordering

    Requires field initializers to follow their containing type's field declaration order by default.