I like to program in flutter in individual lines for each child and it's components, i recently moved to VS Code from Android Studio after having some problems with it, but i noticed that in VS Code, after saving a file (CTRL S) or restarting the IDE, it automatically shrinks the code to one line and it is very frustrating, how could i fix this?
CodePudding user response:
This is not an issue, but the Flutter code formatter works this way. This behaviour is explained in the Flutter documentation:
Flutter code often involves building fairly deep tree-shaped data structures, for example in a build method. To get good automatic formatting, we recommend you adopt the optional trailing commas. The guideline for adding a trailing comma is simple: Always add a trailing comma at the end of a parameter list in functions, methods, and constructors where you care about keeping the formatting you crafted. This helps the automatic formatter to insert an appropriate amount of line breaks for Flutter-style code.
Even though it is not recommended, in VS Code you could disable the automatic code formatting in Preferences -> Settings -> Text Editor -> Formatting
and unchecking the "Format on save" checkbox.
Read more about Flutter code formatting.
CodePudding user response:
If you want flutter to format your code in separate lines, you have to end parameters with comma (,) like so:
Text(
"text",
style: TextStyle(
fontSize: 25,
color: Colors.white,
),
),
If you wish to disable the auto-format feature, just disable the Format On Save
option in vscode settings (Put editor.formatOnSave
in settings search box to find the related setting).