Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart/Flutter prevent formatter from squashing method calls to one line

my formatter keeps doing such a thing. When I try to place method calls in separate lines. For example I have such a code:

main() {
  SomeObject()
    .someMethod()
    .someMethodWithArgument('someArgument')
    .someMethodWithArgument('someOtherArgument');
}

After hitting quick format, I get something like that:

main() {
  SomeObject().someMethod().someMethodWithArgument('someArgument')
    .someMethodWithArgument('someOtherArgument');
}

It drives me crazy and it is totally unreadable in my opinion. I played around with format settings in Android Studio preferences but I cannot find anything that would fix this particular formatting issue.

like image 228
Michał Powłoka Avatar asked Jan 24 '20 19:01

Michał Powłoka


People also ask

What is the best way to ensure consistent formatting in flutter code?

What is the best way to ensure consistent formatting in the Flutter code? DartFMT command replaces the whitespaces with the Dart-guidelines formatting.

How to automatically format Flutter 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. formatOnSave setting to true .

How to format Dart code in Android Studio?

Open Setting > Preferences > Keymap and search for the one that says "Reformat code with 'dart format'". There you can customize it as needed.

Which tool is used for formatting code in 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.


1 Answers

There's no way to configure dartfmt by design. However, you can technically force it to match the formatting you have using comments on each line:

main() {
  SomeObject() //
    .someMethod() //
    .someMethodWithArgument('someArgument') //
    .someMethodWithArgument('someOtherArgument');
}

It's obviously not ideal, and won't be consistent with other Dart code in the ecosystem, but if the formatting bothers you that much it's the only option.

like image 102
Ben Konyi Avatar answered Oct 13 '22 06:10

Ben Konyi