20

How to fix this all const warning in VSCode? It's hard if I fix one by one. enter image description here

5
  • 1
    dart.dev/tools/dart-fix I think this is what you need
    – Simon Sot
    Commented Aug 29, 2021 at 12:21
  • 1
    I've tried it. But, didn't work.
    – caeruleum
    Commented Aug 29, 2021 at 12:45
  • 1
    ctrl + . -> "fix add const everywhere in file"
    – cas
    Commented Aug 29, 2021 at 14:59
  • Thank for helping me. But, I've tried but only one line, not everywhere.
    – caeruleum
    Commented Aug 29, 2021 at 15:59
  • @cas Thanks, that worked for me, just wanted to add in order to get the menu 'fix add const everywhere', we need to select all the code using Ctrl+a(in windows) and Command+a (in mac os) Commented Feb 18 at 10:14

7 Answers 7

26

If you want to add const everywhere in the code, take a look at dart fix and here is a similar question answered.

If you just want to hide all the warnings, you can add

// ignore_for_file: prefer_const_constructors

anywhere in the file.

Or, if you want to get rid of it in all files, find analysis_options.yaml in the root of your project and set the property to false:

enter image description here

If there is no such file (analysis_options.yaml), you can create one and set it to false.

Code the of image file:

  rules: 
    prefer_const_constructors : false
    file_names : false
    public_member_api_docs: false
    lines_longer_than_80_chars: false
    avoid_catches_without_on_clauses: false
    avoid_equals_and_hash_code_on_mutable_classes: false
    prefer_relative_imports: false
    type_annotate_public_apis: false
    avoid_types_on_closure_parameters: false
    sort_constructors_first: false
    prefer_generic_function_type_aliases: true
    unnecessary_lambdas: true
    use_key_in_widget_constructors: false
    avoid_print: false
4
  • 4
    I think he wants to put 'const' to everywhere. Not suppressing the warning. Commented Aug 29, 2021 at 11:52
  • Ah, yes, my bad :-(
    – intraector
    Commented Aug 29, 2021 at 11:57
  • Thanks for your answer. But, I prefer to add const not supress the warning const.
    – caeruleum
    Commented Aug 29, 2021 at 12:44
  • 1
    All const warnings: prefer_const_constructors, prefer_const_constructors_in_immutables, prefer_const_declarations, prefer_const_literals_to_create_immutables Commented Oct 20, 2023 at 14:11
18

Simply right click on any of the warning in the problems tab in vscode and choose Add const modifiers everywhere in the file. enter image description here But you have to do it manually for all files in your project.

Better solution:

Open Vscode : settings -> open settings.json file Copy paste following lines

"editor.codeActionsOnSave": {
    "source.fixAll": true
 }

You can find the settings.json file in 'C:\Users<user-name>\AppData\Roaming\Code\User'

Thats it,From now whenever you're saving a file it will apply the quick fix ( adding const in all places). All you have to do is just save your files.

Note :

It will not only fix the const problem but also fixes some other lint warnings like removing unused imports.

4
  • 1
    is there any solution for Android Studio? Commented Mar 29, 2022 at 17:17
  • 1
    You can use the first option in my answer (Add const modifiers everywhere in the file). Or you can try 'dart fix --dry-run' , 'dart fix --apply'. @MubasharHussain
    – Gopinath
    Commented Mar 30, 2022 at 11:31
  • why am I not seeing the "Add const modifiers everywhere in file" option?
    – Chris
    Commented Jul 20, 2022 at 15:02
  • @Chris if you have more than 1 of these warnings in a single file, then it should show up on a secondary mouse click. Kind of a long shot but, might it be because you don't right-click on one of the said const errors?
    – Han Parlak
    Commented Jul 24, 2022 at 15:24
7

See Flutter Fix

To apply all changes in bulk, run the following command:

dart fix --apply

5

I'm Using visual Studio Code for Flutter app development. you can add

"editor.codeActionsOnSave": {
    "source.fixAll": true
 }

this to Settings.json file and Editor will auto add const and other fixes. for Find Setting.json you have follow this step

Ctrl + Shift + P -> Search Setting.json(Open User Settings.json) -> add above code.
1
  • This method didn't work, but dart fix --apply worked
    – Rajesh
    Commented Jun 3, 2023 at 8:09
3

write it inside terminal dart fix --apply (it will takes some seconds and dart fix fixes deprecated lints in analysis_options.yaml files where possible, by:

removing them, or possibly replacing them with another preferred lint)

0

To apply all changes in bulk, run the following command:

| dart fix --apply

0

first, to preview proposed changes, use the --dry-run flag:

dart fix --dry-run

and then to apply the proposed changes, use the --apply flag:

dart fix --apply

for more information's visit this link: dart-fix https://dart.dev/tools/dart-fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.