'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



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

See Flutter Fix

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

dart fix --apply

Solution 4:[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 -> add above code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gopinath
Solution 3 Conan
Solution 4 Shailandra Rajput