Skip to content

pattern_fields_ordering

v1.0.0WarningConfigurableCode Organization

This rule flags an object or record pattern whose named fields are not in the configured order.

A destructuring pattern reads as a small table of what is being pulled out, and the same object is often destructured in several places. When each spelling picks its own order, the reader cannot compare two of them at a glance to see which fields one takes and the other does not.

A positional field in a record pattern is identified by position and is left alone; one unnamed field makes the whole list unorderable rather than partially ordered.

This rule is in the pedantic preset.

if (user case User(name: final n, age: final a)) {
print('$n is $a');
}
if (user case User(age: final a, name: final n)) {
print('$n is $a');
}

Outside preset: pedantic, which uses alphabetical order, this rule reports nothing until you choose an order:

many_lints.yaml
rules:
pattern_fields_ordering:
order: alphabetical
analysis_options.yaml
many_lints:
rules:
pattern_fields_ordering:
order: alphabetical
Option Type Default Description
order string alphabetical, alphabetical_case_sensitive or by_length. Unset means the rule is silent

To disable this rule:

many_lints.yaml
rules:
pattern_fields_ordering: false

To keep the rule on but skip certain paths, use per-rule exclude.