Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Dart Code formatting problems

I've been trying different configurations in the code-formatting settings from Android-Studio, but the results have been pretty disappointing (and triggering) so far.

This is what the reformat code does without the dartfmt tool (notice line 94 - 110): enter image description here

dartfmt does a better job of it, but the indentation is horrible (line 91 - 96) and very inconsistent with seemingly no settings to configure for it: enter image description here

This is what I would want it to look like: enter image description here

What settings do I need to change to achieve this? It's currently pretty hard to read the code.

like image 415
xorinzor Avatar asked Dec 11 '22 06:12

xorinzor


2 Answers

You can't configure dartfmt. This is voluntary. But dartfmt uses trailing comma as it's core to determine where to go to newline.

The following :

foo({String foo, String bar}) {}

void main() {
  foo(foo: "Hello", bar: "Hello");
}

will stay unchanged when running dartfmt.

But simply changing to foo call to :

  ...bar: "Hello",);

will reformat it to :

foo({String foo, String bar}) {}

void main() {
  foo(
    foo: "Hello",
    bar: "Hello",
  );
}
like image 194
Rémi Rousselet Avatar answered Jan 17 '23 21:01

Rémi Rousselet


It is too late but it can help.

In Android Studio go to

File > Settings > Editor > Code Style > Dart

and increase the value of Line Length.

like image 21
jamal Avatar answered Jan 17 '23 22:01

jamal