Skip to main content
Starter+

Presets

Presets are predefined sets or rules/metrics (for example, for a particular package or for a particular set of problems) that help you quickly configure DCM.

info

While presets are a great way to keep track of rule list updates, upgrading to a newer version of DCM can introduce new issues if a preset has been updated.

Consider this before choosing a preset.

Built-in Presets

To simplify the tool's integration, some presets can be enabled without installing any additional package (for example, the "recommended" preset).

analysis_options.yaml
dart_code_metrics:
extends:
- recommended
note

Since this preset comes with the executable, the list of rules included into it depends on the executable version, but is expected to match the latest version located here.

Additionally, all rule tags are available as built-in presets. For example,

analysis_options.yaml
dart_code_metrics:
extends:
- async
- collections

will enable all these and these rules.

External Presets

Alternatively, you can install the dart_code_metrics_presets package that includes 19 presets:

Enabling an External Preset

To enable a preset:

  1. Install dart_code_metrics_presets as a dev dependency:

    $ flutter pub add --dev dart_code_metrics_presets
  2. For DCM configuration add the extends entry:

    analysis_options.yaml
    dart_code_metrics:
    extends:
    - package:dart_code_metrics_presets/all.yaml
info

If you are reusing a shared configuration between multiple packages, they should all have dart_code_metrics_presets as dev dependency.

info

If the preset cannot be resolved, a preset-not-found warning will be shown.

Reconfiguring a Rule

To reconfigure a rule included into a preset:

analysis_options.yaml
dart_code_metrics:
extends:
- package:dart_code_metrics_presets/all.yaml
rules:
- arguments-ordering:
child-last: true

Disabling a Rule

To disable a rule set its configuration to : false:

analysis_options.yaml
dart_code_metrics:
extends:
- package:dart_code_metrics_presets/all.yaml
rules:
- avoid-banned-imports: false

Defining a Custom Preset

Creating custom presets is also supported. For example,

my_preset/analysis_options.yaml
dart_code_metrics:
extends:
- package:dart_code_metrics_presets/all.yaml
- package:dart_code_metrics_presets/teams_all.yaml

This defines a new preset that will load rules from dart_code_metrics_presets/all.yaml and dart_code_metrics_presets/teams_all.yaml.

To use this custom preset, add it to the extends section of another analysis_options file:

analysis_options.yaml
dart_code_metrics:
extends:
- package:my_presets/analysis_options.yaml

What Structure a Custom Preset Should Have?

The only requirement for a preset is that it should match the regular DCM configuration structure. For example,

analysis_options.yaml
dart_code_metrics:
rules:
- avoid-cubits
- avoid-bloc-public-methods
- avoid-passing-bloc-to-bloc
- prefer-multi-bloc-provider
- prefer-correct-bloc-provider

What Happens If a Rule Is Included Into Multiple Loaded Presets?

When configuration is loaded, the last subsequent entry overrides the previous one, meaning that if in dart_code_metrics_presets/all.yaml there is a rule called some-rule and in my_presets/analysis_options.yaml the same rule is disabled via some-rule: false, in the resulting config the rule will be disabled.

analysis_options.yaml
dart_code_metrics:
extends:
- package:dart_code_metrics_presets/all.yaml
- package:my_presets/analysis_options.yaml # disables 'some-rule' if listed last

Note, that the configuration in the analysis_options.yaml file that loads the presets is the primary configuration and overrides the loaded config:

analysis_options.yaml
dart_code_metrics:
extends:
- package:dart_code_metrics_presets/all.yaml
- package:my_presets/analysis_options.yaml
rules:
- some-rule: true # overrides any configuration from the presets