sort-unnamed-constructors-first

Same as sort_unnamed_constructors_first in Dart lints

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

Flags an unnamed constructor declared after a named constructor.

The unnamed constructor (C() or C.new()) is the primary way to create an instance, so it comes before every named constructor of a class, mixin class, enum, or extension type:

class A {
  A.named();
  A();                       // flagged
}

Only constructors are ordered; fields and methods in between do not matter.

Invalid

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

class B {
  B.a();
  int x = 0;
  B.new();
}

class C {
  factory C.make() => C._();
  C._();
  C();
}

enum E {
  one;

  const E.named();
  const E();
}

extension type Id._(int value) {
  Id.zero() : value = 0;
  Id(this.value);
}
Place the unnamed constructor before named constructors.
2 A.named();
3 A();
Place the unnamed constructor before named constructors.
8 int x = 0;
9 B.new();
Place the unnamed constructor before named constructors.
14 C._();
15 C();
Place the unnamed constructor before named constructors.
21 const E.named();
22 const E();
Place the unnamed constructor before named constructors.
26 Id.zero() : value = 0;
27 Id(this.value);

Valid

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

class B {
  int x = 0;
  B.new();
  B.a();
}

class C {
  C.named();
}

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

enum E {
  one;

  const E();
  const E.named();
}

Configure sort-unnamed-constructors-first

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

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

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

example.dartdart
// falcon-ignore lint/style/sort-unnamed-constructors-first: <reason>
  • 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.

  • unnecessary-breaksunnecessary_breaksrecommended

    Flags a trailing break in a switch case that already ends implicitly.