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

Issue

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.

Solution

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.

Answered By – Ben Konyi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published