Rules / Style / class-members-ordering

class-members-ordering

lint/style/class-members-ordering
recommended
Same as class_members_ordering · Pyramid Lint

Flags class members declared out of a canonical order.

A consistent member layout lets readers find fields, constructors, and methods in the same place across every class. Without options the built-in order applies: static constants, static fields, instance final fields, instance mutable fields, constructors, public getters and setters, public methods, then private members. Members are ranked by category and any pair found out of sequence is reported.

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.

Invalid

example.dartdart
class BadOrdering {
  /// Instance field appears before static const
  int instanceField = 0;

  static const String kAppName = 'MyApp';

  /// Method appears before fields
  void method() {
    print('method');
  }

  static final String kVersion = '1.0.0';

  /// Private field after public methods
  int _privateField = 0;

  /// Constructor after methods
  BadOrdering();

  /// Getter after constructor
  String get name => 'name';

  static var _internalConfig = <String>[];

  void publicMethod() {
    print('public');
  }

  /// Private method
  void _privateMethod() {
    print('private');
  }
}
Class members should be ordered: static const → static fields → instance final → instance var → constructors → public getters/setters → public methods → private members
2 /// Instance field appears before static const
3 int instanceField = 0;
Class members should be ordered: static const → static fields → instance final → instance var → constructors → public getters/setters → public methods → private members
7 /// Method appears before fields
8 void method() {
Class members should be ordered: static const → static fields → instance final → instance var → constructors → public getters/setters → public methods → private members
14 /// Private field after public methods
15 int _privateField = 0;
Class members should be ordered: static const → static fields → instance final → instance var → constructors → public getters/setters → public methods → private members
17 /// Constructor after methods
18 BadOrdering();
Class members should be ordered: static const → static fields → instance final → instance var → constructors → public getters/setters → public methods → private members
20 /// Getter after constructor
21 String get name => 'name';
Class members should be ordered: static const → static fields → instance final → instance var → constructors → public getters/setters → public methods → private members
22
23 static var _internalConfig = <String>[];

Valid

example.dartdart
class GoodOrdering {
  /// Static const first
  static const String kAppName = 'MyApp';
  static const int kMaxRetries = 3;

  /// Static final next
  static final String kVersion = '1.0.0';
  static final List<String> kValidDomains = ['example.com'];

  /// Static var
  static var _internalConfig = <String>[];

  /// Instance final fields
  final String name;
  final int value;

  /// Instance var fields
  String description = '';
  int count = 0;

  /// Constructors: const first, then default, then named
  const GoodOrdering(this.name, this.value);

  GoodOrdering.empty()
      : name = '',
        value = 0;

  GoodOrdering.named({required this.name, this.value = 0});

  /// Factory constructors
  factory GoodOrdering.fromMap(Map<String, dynamic> data) {
    return GoodOrdering(
      data['name'] as String,
      data['value'] as int? ?? 0,
    );
  }

  /// Public getters/setters
  String get displayName => name;

  set description(String value) {
    description = value;
  }

  /// Public methods
  void publicMethod() {
    print('public');
  }

  String process() {
    return 'processed';
  }

  /// Private getter
  bool get _isValid => name.isNotEmpty;

  /// Private methods
  void _privateMethod() {
    print('private');
  }

  void _initialize() {
    count = 0;
  }
}

How to configure

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

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