Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix all const warning flutter

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

like image 215
caeruleum Avatar asked Sep 13 '25 07:09

caeruleum


2 Answers

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
like image 107
intraector Avatar answered Sep 15 '25 21:09

intraector


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.

like image 28
Gopinath Avatar answered Sep 15 '25 19:09

Gopinath