skip to Main Content

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.

3

Answers


  1. 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

    Login or Signup to reply.
  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.

    Login or Signup to reply.
  3. 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.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search