Rules / Style / member-ordering

member-ordering

lint/style/member-ordering
recommended
Same as member-ordering · Dart Code Metrics

Flags class members declared out of the configured order.

A predictable member layout makes classes easier to navigate. Without options the built-in order applies: static constants, static fields, instance fields, constructors, static methods, then instance methods. Members are ranked by category and any pair out of sequence is reported. For Flutter State subclasses, a separate lifecycle ordering can be enforced with widgets_order.

Options

order (list of category tokens, default: the built-in order above) — the required member sequence. Supported tokens are public-fields, private-fields, fields, static-fields, constructors, named-constructors, static-methods, private-methods, public-methods, methods, getters, and setters. A member is ranked by the earliest token it qualifies for; members matching no token are ignored.

widgets_order (list of lifecycle tokens, default: none) — for a class that extends State, orders lifecycle members by this list. Supported tokens: constructor, init-state, did-change-dependencies, did-update-widget, dispose, build, and overridden-methods.

Invalid

example.dartdart
// Test cases for member-ordering rule
// Expected order: static const → static fields → instance fields → constructors → static methods → instance methods

// Instance field after instance method: field is flagged (cat 2 < max 5)
class BadOrderExample1 {
  void instanceMethod() {
    print("method before field");
  }

  final int instanceField = 42;
}

// Static const after instance field: constant is flagged (cat 0 < max 2)
class BadOrderExample2 {
  final int field = 10;

  static const String constant = "const";
}

// Instance field after static method: field is flagged (cat 2 < max 4)
class BadOrderExample3 {
  static void staticMethod() {
    print("static method before instance field");
  }

  final int instanceField = 5;
}

// Instance field after constructor: field is flagged (cat 2 < max 3)
class BadOrderExample4 {
  int field1 = 1;

  BadOrderExample4();

  int field2 = 2;
}

// Static const and instance field after instance method: both flagged
class BadOrderExample5 {
  void method1() {
    print("method");
  }

  static const int constant = 42;

  final int field = 100;
}

// Constructor after instance method: constructor is flagged (cat 3 < max 5)
class BadOrderExample6 {
  void doSomething() {
    print("do");
  }

  BadOrderExample6() {
    print("constructor");
  }

  final String name = "test";
}

// Static const after instance method: constant is flagged (cat 0 < max 5)
class BadOrderExample7 {
  static int staticField = 10;

  static void staticMethod() {
    print("static method");
  }

  void instanceMethod() {
    print("instance method");
  }

  static const int constant = 5;
}
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
9
10 final int instanceField = 42;
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
16
17 static const String constant = "const";
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
25
26 final int instanceField = 5;
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
34
35 int field2 = 2;
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
43
44 static const int constant = 42;
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
45
46 final int field = 100;
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
54
55 BadOrderExample6() {
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
58
59 final String name = "test";
Class members are out of order: expected static const → static fields → instance fields → constructors → static methods → instance methods
73
74 static const int constant = 5;

Valid

example.dartdart
// Good examples for member-ordering rule
// Correct order: static const → static fields → instance fields → constructors → static methods → instance methods

class WellOrderedExample1 {
  static const String constant = "const";
  static int staticField = 10;

  final int instanceField = 42;

  WellOrderedExample1() {
    print("constructor");
  }

  static void staticMethod() {
    print("static method");
  }

  void instanceMethod() {
    print("instance method");
  }
}

class WellOrderedExample2 {
  static const int maxSize = 100;

  int count = 0;
  String name = "test";

  WellOrderedExample2() {
    count = 0;
  }

  static int defaultValue() {
    return 42;
  }

  void increment() {
    count++;
  }

  void reset() {
    count = 0;
  }
}

class WellOrderedExample3 {
  static const List<String> defaults = [];
  static bool enabled = true;

  final String id;
  late String value;

  WellOrderedExample3(this.id) {
    value = id.toUpperCase();
  }

  WellOrderedExample3.named(String name) {
    id = name;
    value = name;
  }

  static void logDefaults() {
    print("defaults");
  }

  void process() {
    print(value);
  }

  @override
  String toString() => id;
}

class SimpleClass {
  static const String defaultName = "default";

  String name;

  SimpleClass([this.name = defaultName]);

  static String getDefault() => defaultName;

  void changeName(String newName) {
    name = newName;
  }
}

How to configure

Set the severity of member-ordering in your falcon.json:

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