mirkancal
mirkancal

Reputation: 5355

How to fix auto-fixable problems on VSCode with Flutter?

I've updated pedantic package and ended up 1K+ problems. Many of them are prefer_single_quotes or unnecessary this etc. They are one click away to fix. I think eslint or some other tools can do that within the VSCode.

Is there a way to do it with Flutter projects? I find dartfix package but says “No recommended changes” after running it. What I want to do is fix all the auto-fixable problems on the problems tab.

dartfix on pub.dev

Upvotes: 18

Views: 20336

Answers (7)

Sireen Ibnu Kabeer
Sireen Ibnu Kabeer

Reputation: 48

This might help someone in future,

adding this in your settings.json will fix the code as well as organises the imports on save.

{
  //...
  "editor.codeActionsOnSave": {
    "source.fixAll": "always",
    "source.organizeImports": "always"
  },
}

Upvotes: 0

ivansaul
ivansaul

Reputation: 3332

For fix all Analysis issues identified by dart analyzer, you can use the vscode flutter fix all command.

In your project folder, create a .vscode/settings.json file and add the following:

{
    // ...
   "editor.codeActionsOnSave": {
        "source.fixAll": "explicit",
    },
}

Upvotes: 3

Manbus
Manbus

Reputation: 1026

Honestly I tried all fixes listed on here, and the one that helped me the most was manually using the search and replace feature in VSCode. For example, if you have 500+ Invalid const value errors, just search for something like "const Text(" and replace it with "Text(" and all the errors with this error assorsiated with text will be gone... I just repeated this with other errors and then at the end, run:

dart fix --dry-run && dart fix --apply

and this got rid of my 500+ errors and most of the problems listed. Hope this works. Just get creative with your search and replaceAll phase...

Upvotes: 3

Abhishek Patil
Abhishek Patil

Reputation: 360

You can fix all the Analysis issues identified by dart analyze on save of file.

Add this in settings.json file

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

Upvotes: 19

James
James

Reputation: 673

Not sure if you have tried the built in fixes using dart fix.

Use dart fix --dry-run to see suggested changes and dart fix --apply to apply them.

https://dart.dev/tools/dart-fix

Upvotes: 39

Jufe Brown-Tsai
Jufe Brown-Tsai

Reputation: 19

I was having the same issue and tried dartfix. It worked for me when I specified the correct path:

$ dartfix --pedantic lib/src/ --overwrite

Went from ~450 problems to 59

Even though all of my files that I wanted to fix are in the src folder, I had to run the command again with $ dartfix --pedantic lib/ --overwrite to get my main file. That might be the issue that you're having, too.

Upvotes: 1

SugaR256
SugaR256

Reputation: 1039

I'm not sure how many of these problems dartfmt can cover, but it's definitely worth trying! To run dartfmt with idiomatic fixing, overwriting and link-following, please run dartfmt --fix --overwrite --follow-links . in your project folder.

Upvotes: 0

Related Questions