Skip to main content

avoid-hardcoded-numbers

effort: 2m
configurable
pro+

Warns when a configured class invocation has an argument with a hardcoded number literal.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config​

Set entries (default is empty) to configure the list of class invocations.

Each entry is an object with 2 fields: name and args:

  • Set name to configure the name of the invocation.
  • Set args to configure a list of arguments that should not be a hardcoded number literal.
analysis_options.yaml
dcm:
rules:
- avoid-hardcoded-numbers:
entries:
- name: Column
args:
- spacing
- name: SomeClass.named
args:
# if there is no "padding" named argument,
# the positional argument with the same name will be checked instead
- padding

Example​

❌ Bad:

Column(
// LINT: Avoid hardcoded numbers. Try using an existing constant or variable.
spacing: 10,
children: [
...
],
);

Column(
// LINT: Avoid hardcoded numbers. Try using an existing constant or variable.
spacing: 10.5,
children: [
...
],
);

✅ Good:

const _spacing = 10;

Column(
spacing: _spacing,
children: [
...
],
);