Skip to main content

member-ordering

added in: 1.6.0
⚙️🛠
Free+

Enforces member ordering.

info

By default, this rule works only for classes. To lint other declarations, see the configuration below.

⚙️ Config

Set ignore-mixins (default is true) to exclude mixins from checks.

Set ignore-extensions (default is true) to exclude extensions from checks.

Set ignore-extension-types (default is true) to exclude extension types from checks.

Set ignore-enums (default is true) to exclude enums from checks.

Order configuration

Set order to configure the order for regular classes.

Set widgets-order to configure the order for widget classes.

The value for the order or widgets-order entries should match the following pattern:

< (overridden | protected)- >< (private | public)- >< static- >< late- >< (var | final | const)- >< nullable- >< named- >< factory- > (fields | getters | getters-setters | setters | field-getter-setter | constructors | methods)

where values in the <> are optional, values in the () are interchangeable and the last part of the pattern which represents a class member type is REQUIRED.

You can also apply order to a separate method, field or constructor by listing its name like:

  • build-method
  • dispose-method
  • init-state-method
  • my-custom-cool-thing-method
  • from-json-constructor
  • custom-field
  • hash-code-getter
info

Not all of the pattern parts are applicable for every case, for example, late-constructors are not expected, since they are not supported by the language itself.

field-getter-setter is a synthetic group that ignores any applied modifier.

For example, the value for order or widgets-order may be an array consisting of the following strings:

  • public-late-final-fields
  • private-late-final-fields
  • public-nullable-fields
  • private-nullable-fields
  • named-constructors
  • factory-constructors
  • getters
  • setters
  • public-static-methods
  • private-static-methods
  • protected-methods
  • etc.

You can simply configure the rule to sort only by a type:

  • fields
  • methods
  • setters
  • getters (or just getters-setters if you don't want to separate them)
  • field-getter-setter (groups fields, getters and setters with the same name)
  • constructors

The default config for order is:

  • public-fields
  • private-fields
  • public-getters
  • private-getters
  • public-setters
  • private-setters
  • constructors
  • public-methods
  • private-methods

The default config for widgets-order is (from Flutter style guide):

  • constructor
  • named-constructor
  • const-fields
  • static-methods
  • final-fields
  • var-fields
  • init-state-method
  • private-methods
  • overridden-public-methods
  • build-method

The alphabetize option will enforce that members within the same category should be alphabetically sorted by name.

The alphabetize-by-type option will enforce that members within the same category should be alphabetically sorted by theirs type name.

note

Only one alphabetize option could be applied at the same time.

⚙️ Config example

For all declarations:

analysis_options.yaml
dart_code_metrics:
rules:
- member-ordering:
ignore-mixins: false
ignore-extensions: false
ignore-extension-types: false
ignore-enums: false

With the default config:

analysis_options.yaml
dart_code_metrics:
rules:
- member-ordering

OR with a custom one:

analysis_options.yaml
dart_code_metrics:
rules:
- member-ordering:
alphabetize: true
order:
- public-fields
- private-fields
- constructors

OR Flutter specific:

analysis_options.yaml
dart_code_metrics:
rules:
- member-ordering:
widgets-order:
- build-method
- init-state-method
- did-change-dependencies-method
- did-update-widget-method
- dispose-method

OR both custom and Flutter specific:

analysis_options.yaml
dart_code_metrics:
rules:
- member-ordering:
order:
- public-fields
- private-fields
- constructors
- close-method
- dispose-method
widgets-order:
- constructor
- build-method
- init-state-method
- did-change-dependencies-method
- did-update-widget-method
- dispose-method