member-ordering
Enforces member ordering.
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.
Default values
The default config for order
is:
- public-fields
- private-fields
- public-getters
- private-getters
- public-setters
- private-setters
- constructors
- public-methods
- private-methods
Set widgets-order
to configure the order for widget classes.
Default values
The default config for widgets-order
is (from Flutter style guide):
- constructors
- named-constructors
- const-fields
- static-methods
- final-fields
- var-fields
- init-state-method
- private-methods
- overridden-public-methods
- build-method
Supported optional modifiers (can be used in any order):
overridden-
orprotected-
(for fields, methods and getters/setters)public-
orprivate-
(for all members)external-
(for all members)operator-
(for methods only)static-
(for fields, methods and getters/setters)late-
(for fields only)var-
orfinal-
orconst-
(for fields only)initialized-
(for fields only)abstract-
(for fields and methods)nullable-
(for all members)named-
(for constructors only)factory-
(for constructors only)redirecting-
(for constructors only)
Supported groups (required):
- fields / getters / getters-setters / setters / field-getter-setter / constructors / methods
To get a valid config entry, combine modifiers and a group name (should always be last):
dart_code_metrics:
rules:
- member-ordering:
alphabetize: true
order:
- public-late-final-fields
- final-initialized-fields
- static-methods
- redirecting-constructors
- named-constructors
field-getter-setter
is a synthetic group that ignores any applied modifier.
Additionally, to apply a specific order to a particular method, field or constructor, specify its name as follows:
- build-method
- dispose-method
- init-state-method
- my-custom-cool-thing-method
- from-json-constructor
- custom-field
- hash-code-getter
Named entries use the singular form (e.g. -method
), while general entries use the plural form (e.g. -methods
).
Not specifying any modifiers also works:
- 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 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.
Only one alphabetize option could be applied at the same time.
⚙️ Config Example
For all declarations:
dart_code_metrics:
rules:
- member-ordering:
ignore-mixins: false
ignore-extensions: false
ignore-extension-types: false
ignore-enums: false
With the default config:
dart_code_metrics:
rules:
- member-ordering
OR with a custom one:
dart_code_metrics:
rules:
- member-ordering:
alphabetize: true
order:
- 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
OR Flutter specific:
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:
dart_code_metrics:
rules:
- member-ordering:
order:
- public-fields
- private-fields
- constructors
- close-method
- dispose-method
widgets-order:
- constructors
- build-method
- init-state-method
- did-change-dependencies-method
- did-update-widget-method
- dispose-method