How to fix a specific lint warning for the entire project?

Viewed 526

There are many lint warnings in different files of my project like:

Prefer const with constant constructors.
Use key in widget constructors.
...
Unnecessary string interpolation. 

Is there a way to only fix a particular warning, something like

dart fix prefer_const_constructors 

PS: I don't want to fix all the warnings, for that I can run dart fix --apply.

2 Answers

Yes, It is possible by changing lint rules. For time being, you have to add only rules which you want to fix and ignore all others.

Follow these steps

In the project, you have to create analysis_options.ymal file. The content of the file will look like this.

linter:
  rules:
    prefer_const_constructors: true

More details here

After that try to run dart fix, since only one lint rule is enabled it only gives you suggestions for that only.

To ignore a single line, you can add a comment above the line:

// ignore: non_constant_identifier_names
final NEW = 'NEW';

To ignore for the whole file, you can add a comment at the top of the file:

// ignore_for_file: non_constant_identifier_names

To ignore for the whole project, you can set the rule to false in your analysis_options.yaml file:

include: package:lints/recommended.yaml

linter:
  rules:
    non_constant_identifier_names: false

Refer this for more

Related