Search code examples
flutterandroid-studiodartsyntaxcurly-braces

Flutter Android Studio make curly braces start on new line


I've been trying to set my curly braces for Flutter in Android Studio. I tried looking in the project settings but Dart doesn't offer style changes. At the moment, my code looks like this:

class Trial extends StatefulWidget {
  const Trial({Key key}) : super(key: key);

  @override
  _TrialState createState() => _TrialState();
}

class _TrialState extends State<Trial> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

but I want it to look like this all the time

class Trial extends StatefulWidget 
{
  const Trial({Key key}) : super(key: key);

  @override
  _TrialState createState() => _TrialState();
}

class _TrialState extends State<Trial>
{
  @override
  Widget build(BuildContext context) 
  {
    return Container();
  }
}

How do I make the auto-formatter create curly braces on a new line?


Solution

  • Dart Tries to format the code itself so that it is consistent for every developer not just you. So that everyone understands the code same way and has no confusion.

    Code is formatted as per dartfmt tool from dart sdk. As per documentation in code styling at https://github.com/dart-lang/dart_style/wiki/FAQ It Describes two points :

    I don't like the output!

    First of all, that's not a question. But, yes, sometimes you may dislike the output of the formatter. This may be a bug or it may be a deliberate stylistic choice of the formatter that you disagree with. The simplest way to find out is to file an issue.

    Now that the formatter is pretty mature, it's more likely that the output is deliberate. If you still aren't happy with what it did to your code, the easiest thing you can do is tweak what you send it. While the formatter tries to make all code look great, there are trade-offs in some of the rules. In those cases, it leans towards making more common idioms look better.

    If your code ends up looking bad, your code may be off the beaten path. Usually hoisting an expression up to a local variable or taking a big lambda out and making it a named function is all that's need to get back to a happy place.

    Why can't I configure it?

    The formatter supports very few tweakable settings, by design. If you look up at the list of priorities above, you'll see configurability goes directly against the first two priorities, and halfway against the third (you have to think about it, but not apply it).

    This may be surprising, but the goal of dartfmt is not to automatically make your code look the way you like. It's to make everyone's Dart code look the same. The primary goal of dartfmt is to improve the quality of the Dart ecosystem. That transitively improves the live's of each Dart developer as well—you get more code to reuse, more consistent code, it's easier to read and contribute to each other's code, etc. But it does that at the expense of individual preference.

    It requires you to buy in to the idea that a consistent ecosystem is more valuable than anyone's individual formatting preferences. Or, another way to say that is that no one's individual formatting style is measurably better enough than what dartfmt produces to compensate for the costs of inconsistency and having to argue over what your team's house style should be.

    If you don't buy that, that's OK. It just means dartfmt probably isn't the right tool for you.