19

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

caeruleum
  • 459
  • 1
  • 3
  • 16

5 Answers5

18

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
intraector
  • 994
  • 10
  • 20
17

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.

Gopinath
  • 214
  • 2
  • 5
  • 1
    is there any solution for Android Studio? – Mubashar Hussain Mar 29 '22 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 Mar 30 '22 at 11:31
  • why am I not seeing the "Add const modifiers everywhere in file" option? – Chris Jul 20 '22 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? – KHAN Jul 24 '22 at 15:24
6

See Flutter Fix

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

dart fix --apply

Conan
  • 101
  • 1
  • 3
4

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.
Shailendra Rajput
  • 2,131
  • 17
  • 26
2

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)