VSCode Flutter refuses to indent with 4 spaces
Asked Answered
N

1

7

I find it incredibly distracting to code with 2 spaces indentation, so I always set my indentation size to 4 spaces, however, on this flutter project, the indentation stays at 2 spaces, no matter what I change, it refuses to be 4 spaces... (this applies to all files, not just the main.dart file)

Example of spaces in my code

I've tried setting indentation spaces to 4 from 2 (https://mcmap.net/q/109299/-visual-studio-code-format-is-not-using-indent-settings and https://mcmap.net/q/235159/-how-do-i-change-vscode-to-indent-4-spaces-instead-of-default-2) but even when it is set to 4 spaces for Dart specific projects (the json file), workspace settings (assuming frontend for json file) and in the VSCode user settings tab, it goes back to 2 spaces on format (or rather the settings stays at 4 spaces, but it still formats at 2 spaces). I do not have a .editorconfig, so I couldn't change it there. I've also disabled detect indentations (https://mcmap.net/q/109299/-visual-studio-code-format-is-not-using-indent-settings) on all the levels I did with the indentations sizes.

JSON settings

User settings

VSCode spaces display preview

Could it be that dart_style (which I am assuming is formatting the code), enforces 2 spaces indentations? If it is, is it possible to change it?

dart style enabled

Although, taking a look through its code, it shouldn't be enforcing 2 spaces, like this function. (see image)

Code Snippet

I also only have these 2 extensions for Dart/Flutter, and only Prettier formatter (only other formatter), but disabling it does not fix the issue...

Extensions

Nomanomad answered 17/11, 2022 at 16:48 Comment(5)
Tbh with how flutter is structured it's better to use 2 spaces.Telephonic
@Telephonic Yeah, I guess, if I can't change it, then I'll make do, it's not the end of the world, just an annoyanceNomanomad
it also annoyed hell out of me XD. But later I noticed how "hilly" flutter code is.Telephonic
I fail to see "with how flutter is structured it's better to use 2 spaces". How does indenting with 4 spaces make one iota of difference to the language? I just started looking at Flutter but forcing me to not see the code indentation clearly enough is going to make life a living hell.Renz
I am surprised tabs aren't used. At least with a tab, people can choose how much space a tab takes, 2, 4, 8. Whitespace is important. It is exhausting reading Flutter. After a while, all the code just looks the same "WidgetFormchildchildrenconstContainer". I imagine Flutter is very difficult on people with vision impairments.Tecumseh
J
5

For those who tired to wait a customisation from Dart guys here is a solution: fork the dart_style repository, change to own needs and setup the replacement to be used in VSCode. Instruction (and ready patch) is here

Jeffereyjefferies answered 4/12, 2023 at 10:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.