Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change dart line length in vscode when formatting dart files?

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?

like image 833
alireza easazade Avatar asked Dec 23 '19 13:12

alireza easazade


People also ask

How do you fix line length in VS code?

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.

How do you format a Dart code in VS code?

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.

Which tool is used for formatting Dart code?

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.

Does prettier work with Dart?

VSCode: Prettier does not work with Dart Flutter.

How do I format a DART file?

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.

Is it safe to use Dart format?

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.

How to format the entire file in VSCode?

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.

How do I replace the whitespace in my program using Dart?

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.


2 Answers

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.

like image 116
Niek Avatar answered Sep 30 '22 12:09

Niek


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.

VSCode Dart&Flutter extension settings - line length

like image 38
pr0gramist Avatar answered Sep 30 '22 11:09

pr0gramist