sort-constructors-first

Same as sort_constructors_first in Dart lints

Group
Style
Path
lint/style/sort-constructors-first
Default
Off, opt in
Origin
Dart lints
Rule
437 of 512

Flags a constructor declared after a non-constructor member.

Constructors describe how an instance comes to be, so they lead the member list of a class, mixin class, enum, or extension type:

class A {
  A();
  int x = 0;
  A.named();                 // flagged
}

Each out-of-place constructor is reported at its name. Enum values are not members, so they never count as preceding declarations. Primary constructor headers do not participate in body ordering; their explicit this bodies do, and are reported at the this keyword.

Invalid

example.dartdart
class A {
  A();
  int x = 0;
  A.named();
  factory A.make() => A();
}

class B {
  void m() {}
  B();
}

enum E {
  one, two;

  int get value => index;
  const E();
}

abstract class M {
  static const k = 1;
  M();
}

extension type Id(int value) {
  int get doubled => value * 2;
  Id.zero() : value = 0;
}
Constructor declarations should be before non-constructor declarations.
3 int x = 0;
4 A.named();
Constructor declarations should be before non-constructor declarations.
4 A.named();
5 factory A.make() => A();
Constructor declarations should be before non-constructor declarations.
9 void m() {}
10 B();
Constructor declarations should be before non-constructor declarations.
16 int get value => index;
17 const E();
Constructor declarations should be before non-constructor declarations.
21 static const k = 1;
22 M();
Constructor declarations should be before non-constructor declarations.
26 int get doubled => value * 2;
27 Id.zero() : value = 0;

Valid

example.dartdart
class A {
  A();
  A.named();
  factory A.make() => A();
  int x = 0;
  void m() {}
}

class OnlyMembers {
  int x = 0;
  void m() {}
}

enum E {
  one, two;

  const E();
  int get value => index;
}

mixin M {
  int field = 0;
}

extension type Id(int value) {
  Id.zero() : value = 0;
  int get doubled => value * 2;
}

Configure sort-constructors-first

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "sort-constructors-first": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/sort-constructors-first: <reason>
  • sort-unnamed-constructors-firstsort_unnamed_constructors_first

    Flags an unnamed constructor declared after a named constructor.

  • tighten-type-of-initializing-formalstighten_type_of_initializing_formals

    Flags a nullable this.x or super.x initializing formal whose constructor asserts x != null.

  • type-annotate-public-apistype_annotate_public_apisrecommended

    Requires type annotations on public declarations.

  • type-init-formalstype_init_formalsrecommended

    Flags a *redundant* type annotation on an initializing formal or super parameter (type-init-formals, adopted from package:lints).

  • unawaited-futuresunawaited_futuresrecommended

    Future results in async bodies must be awaited or marked unawaited.

  • unintended-html-in-doc-commentunintended_html_in_doc_commentrecommended

    Flags angle-bracket text in doc comments that Markdown would render as an HTML tag.

  • unnecessary-await-in-returnunnecessary_await_in_return

    Avoid await on a returned Future the function's own Future already covers.

  • unnecessary-brace-in-string-interpsunnecessary_brace_in_string_interpsrecommended

    Flags ${x} string interpolations whose braces can be dropped.