initializers-ordering

Group
Style
Path
lint/style/initializers-ordering
Default
Off, opt in
Origin
Falcon
Rule
246 of 512

Requires field initializers to follow their containing type's field declaration order by default. Explicit order: ascending or descending selects alphabetical ordering. Only literal values and constructor-parameter reads can be reordered because initializer expressions evaluate in written order. Other expressions, assertions, and super/this constructor calls keep their positions and break ordering runs; unknown fields also break declaration-order runs rather than guessing their order.

Invalid

example.dartdart
class Example {
  final int zebra;
  final int alpha;
  Example() : alpha = 1,  zebra = 2;
  Example.values(int a, int z)
      : alpha = a,  zebra = z;
}

class Words {
  final String zebra, alpha;
  Words() : alpha = 'a',  zebra = 'z';
}

class WithBoundary {
  final int zebra;
  final int alpha;
  WithBoundary()
      : assert(true),
        alpha = 1,
         zebra = 2;
}

enum EnumFields {
  value;

  final int zebra;
  final int alpha;
  const EnumFields()
      : alpha = 1,  zebra = 2;
}
Reorder field initializers to follow field declaration order.
3 final int alpha;
4 Example() : alpha = 1, zebra = 2;
Reorder field initializers to follow field declaration order.
5 Example.values(int a, int z)
6 : alpha = a, zebra = z;
Reorder field initializers to follow field declaration order.
10 final String zebra, alpha;
11 Words() : alpha = 'a', zebra = 'z';
Reorder field initializers to follow field declaration order.
19 alpha = 1,
20 zebra = 2;
Reorder field initializers to follow field declaration order.
28 const EnumFields()
29 : alpha = 1, zebra = 2;

Valid

example.dartdart
int next() => 1;

class Example {
  final int zebra;
  final int alpha;
  Example() : zebra = 2, alpha = 1;
  Example.values(int a, int z) : zebra = z, alpha = a;
  Example.withAssert() : alpha = 1, assert(true), zebra = 2;
  Example.withEffects() : zebra = next(), alpha = next();
  Example.withUnknownValue() : zebra = next(), alpha = 1;
  Example.reversedEffects() : alpha = next(), zebra = next();
  Example.reversedUnknownValue() : alpha = next(), zebra = 1;
  Example.redirect() : this();
}

class Parent {
  Parent();
}

class Child extends Parent {
  final int zebra;
  final int alpha;
  Child() : zebra = 2, alpha = 1, super();
  Child.withAssert() : alpha = 1, assert(true), zebra = 2, super();
}

class Words {
  final String zebra, alpha;
  Words() : zebra = 'z', alpha = 'a';
  Words.withInterpolation() : zebra = '${next()}', alpha = 'a';
  Words.reversedInterpolation() : alpha = '${next()}', zebra = 'z';
}

// Each containing class has its own declaration order.
class Opposite {
  final int alpha;
  final int zebra;
  Opposite() : alpha = 1, zebra = 2;
}

enum EnumFields {
  value;

  final int zebra;
  final int alpha;
  const EnumFields() : zebra = 2, alpha = 1;
}

Configure initializers-ordering

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "initializers-ordering": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/initializers-ordering: <reason>
  • leading-newlines-in-multiline-stringsleading_newlines_in_multiline_strings

    Flags a multiline string spanning several lines whose text does not start with a line break right after the opening quotes.

  • library-annotationslibrary_annotationsrecommended

    Requires library-targeted metadata to attach to a library directive.

  • library-nameslibrary_namesrecommended

    Flags a library name that is not lowercase_with_underscores.

  • library-prefixeslibrary_prefixesrecommended

    Flags import prefixes that are not lower_case_with_underscores.

  • lines-longer-than-80-charslines_longer_than_80_chars

    Flags source lines longer than 80 characters.

  • map-keys-ordering

    Flags literal string map keys that violate the configured order.

  • matching-super-parametersmatching_super_parameters

    Flags positional super parameters named differently from the super constructor's corresponding parameter.

  • member-orderingmember-ordering

    Flags class members declared out of the configured order.