i'm using VS Code for flutter development and one issue i have is code formatting (styling) which is not as customizable as it is in android-studio. my problem is that each time vs code saves dart files it will break my lines very short like below:
var tuple = settings.arguments as Tuple3<String, int, Field>;
obviously this is what i want :
var tuple = settings.arguments as Tuple3<String, int, Field>;
how can i solve this problem?
Open Extensions on the menu on the left or File menu. Then click on the extension settings button. Change the line length for the extension you are working with.
Automatically formatting code in VS Code To automatically format the code in the current source code window, right-click in the code window and select Format Document . You can add a keyboard shortcut to this VS Code Preferences. To automatically format code whenever you save a file, set the editor.
The dart_style package defines an automatic, opinionated formatter for Dart code. It replaces the whitespace in your program with what it deems to be the best formatting for it.
VSCode: Prettier does not work with Dart Flutter.
Fortunately, you can use the dart format tool — from the command line or in your favorite Dart-savvy IDE — to perform most of the drudge work of formatting your code. For example, here’s how to format all the Dart files under the current directory’s bin, lib, and test directories: However, dart format can’t do it all.
However, dart format can’t do it all. To avoid making changes that might be unsafe, dart format affects only whitespace. For additional guidance, see the Effective Dart style guidelines, especially the formatting guidelines.
It formats the entire file, You can also use shortcut code Shift+Alt+F. you can select a block of code or entire file code that formats the selected code. You can use Ctrl+K Ctrl+F. It works in Windows. Sometimes shortcuts are not working in Windows. How to customize format settings in VSCode? Here are the steps to change default settings.
Use the dart format command to replace the whitespace in your program with formatting that follows Dart guidelines . This is the same formatting that you can get when using an IDE or editor that has Dart support. For more information about this and other dart commands, see the Dart command-line tool page.
You need to change 2 settings in settings.json:
"dart.lineLength": 150, "[dart]": { "editor.rulers": [ 150 ], }
If you do not change the second one, you'll still see the vertical "ruler" at 80 chars width.
It seems like you are hitting line length limit.
Default maximum line length is classic 80 characters, so for your code you would need a lot of padding to hit the limit so formatter would break the line. If this is an issue - consider splitting your code.
This is properly formatted:
class MyApp { void insideclass() { if (true) { if (true) { if (true) { if (true) { if (true) { if (true) { if (true) { if (true) { var tuple = settings.arguments as Tuple3<String, int, Field>; } } } } } } } } } } class MyApp2 { void insideclass() { if (true) { if (true) { if (true) { if (true) { if (true) { if (true) { if (true) { var tuple = settings.arguments as Tuple3<String, int, Field>; } } } } } } } } }
However if 80 is actually too small for you, you can also change that in VSCode in the extension's settings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With