简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM