Skip to main content

parameters-ordering

added in: 1.9.0
⚙️🛠
Pro+

Ensures consistent alphabetical order of parameters by their names.

warning

When applying quick fixes for this rule, the arguments passed to invocations do not get updated automatically.

⚙️ Config

Set default (default is unset, can be set to first, last or unset) to configure ordering rules for parameters with default values.

Set super (default is unset, can be set to first, last or unset) to configure ordering rules for super parameters.

Set required (default is unset, can be set to first, last or unset) to configure ordering rules for required parameters (example).

Set ignore-overridden (default is false) to exclude overridden methods.

Set only-named (default is false) to check only the named parameters.

analysis_options.yaml
dart_code_metrics:
rules:
- parameters-ordering:
default: 'unset'
super: 'unset'
required: 'unset'
ignore-overridden: false
only-named: false
info

If several entries have the same value (for example, "default" and "required" are both "first"), then the order in which the configuration entries are listed in the rule's config defines which of them is applied first.

Example

❌ Bad:

void named({String b, String a}) {} // LINT: Parameter names do not match the alphabetical order. Try sorting them.

void optional([String? b, String? a]) {} // LINT: Parameter names do not match the alphabetical order. Try sorting them.

void mixed(String b, String a, {int c}) {} // LINT: Parameter names do not match the alphabetical order. Try sorting them.

void anotherMixed(String a, String b, [int d, int c]) {} // LINT: Parameter names do not match the alphabetical order. Try sorting them.

✅ Good:

void namedCorrect({String a, String b}) {} // Correct, alphabetical order

void optionalCorrect([String? a, String? b]) {} // Correct, alphabetical order

void mixedCorrect(String a, String b, {int c}) {} // Correct, alphabetical order

void mixedAnotherCorrect(String a, String d, {int c}) {} // Correct, d is a positional parameter

Example with "required"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- parameters-ordering:
required: 'first'

❌ Bad:

// LINT: Parameter names do not match the alphabetical order. Try sorting them.
void withRequired(
int a, {
required String d,
String c,
required String b,
}) {}

✅ Good:

void withRequired(
int a, {
required String b, // Correct, required parameters come first
required String d,
String c,
}) {}