5

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.

iDecode
  • 22,623
  • 19
  • 99
  • 186

3 Answers3

2

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.

Vivek Bansal
  • 1,301
  • 13
  • 21
2

You can now use the flag --code for the newest dart version

command will be like: dart fix --apply --code prefer_const_constructors

%dart fix --help
Apply automated fixes to Dart source code.

This tool looks for and fixes analysis issues that have associated     automated fixes.

To use the tool, run either 'dart fix --dry-run' for a preview of the proposed changes for a project, or 'dart fix --apply' to apply the changes.

Usage: dart fix [arguments]
-h, --help                      Print this usage information.
-n, --dry-run                   Preview the proposed changes but make no changes.
--apply                     Apply the proposed changes.
--code=<code1,code2,...>    Apply fixes for one (or more) diagnostic codes.

Run "dart help" to see global options.
yellowgray
  • 4,006
  • 6
  • 28
-2

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

  • Thanks but this wasn't the question about. I don't want to [suppress the warning](https://stackoverflow.com/a/69531249/12483095). – iDecode Nov 24 '21 at 13:11
  • According to the [official documentation](https://docs.flutter.dev/development/tools/flutter-fix), they haven't mentioned a way to fix specifically selected lint warnings using one code line. – Sankalpa Senevirathne Nov 24 '21 at 14:31
  • Yes, I know which is why I asked the question, if there is *any way* or *workaround* of doing it. – iDecode Nov 24 '21 at 15:18