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

FlutterDartVisual Studio-CodeFormattingCoding Style

Flutter Problem Overview


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?

Flutter Solutions


Solution 1 - Flutter

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.

Solution 2 - Flutter

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

Solution 3 - Flutter

To change the line length in VSCode

open settings.json and add the following lines

"dart.lineLength": 120,
"[dart]": {
    "editor.rulers": [
        120
    ],
}

SIDE NOTE: if you wish to change the dart line length for a single project that you have in VSCode create a .vscode/settings.json file and add configuration written above in that file.

to change dart line length in Android Studio go to

Settings > Editor > Code Style > Dart and change line length

enter image description here

Solution 4 - Flutter

For Android Studio

Android Studio -> Preferences -> Editor -> Code Style -> Dart -> Line length

Solution 5 - Flutter

Its a line kind of to show you where you should cut your code just change the "editor.rulers: [ 150 ]" in setting.json which you can find if you go to setting and search for example font ligature then click the edit in settings text which is underlined but there are many ways to find it of course then it will disappear. Its so annoying to look at.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionalireza easazadeView Question on Stackoverflow
Solution 1 - FlutterNiekView Answer on Stackoverflow
Solution 2 - Flutterpr0gramistView Answer on Stackoverflow
Solution 3 - Flutteralireza easazadeView Answer on Stackoverflow
Solution 4 - FlutterRamazan TimurView Answer on Stackoverflow
Solution 5 - FlutterAbrham DanielView Answer on Stackoverflow