How to fix this all const warning in VSCode? It's hard if I fix one by one.
7 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:
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
-
4I think he wants to put 'const' to everywhere. Not suppressing the warning. Commented Aug 29, 2021 at 11:52
-
-
Thanks for your answer. But, I prefer to add const not supress the warning const. Commented Aug 29, 2021 at 12:44
-
1All 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
Simply right click on any of the warning in the problems tab in vscode and choose Add const modifiers everywhere in the file. 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.
-
1
-
1You 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– GopinathCommented Mar 30, 2022 at 11:31
-
why am I not seeing the "Add const modifiers everywhere in file" option?– ChrisCommented 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? Commented Jul 24, 2022 at 15:24
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.
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)
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
ctrl + .
-> "fix add const everywhere in file"