The only ugly part of the dart formatter

Hey folks,

The dart formatter is awesome and I also see the clear advantages that dart code is formatted the same for all developers. But there is always one thing that my eyes can’t get used to and in my opinion dart doesn’t have a reasonable default here.
Chain a series of functions and assign the result to a variable. If the line length is exceeded, the assignment is wrapped, instead of splitting the chain into individual lines.

    final someIndexedData = <String, ({String jobId})>{};

    // The ugly Default:
    final job1 =
        someIndexedData.values.where((job) => job.jobId == '3').firstOrNull;

    // Tweak with tailing comma, but still ugly
    final job2 = someIndexedData.values
        .where(
          (job) => job.jobId == '3',
        )
        .firstOrNull;

    // The desired formatting can only be achieved if the second line 
    // also exceeds the line limit. 
    final job3 = someIndexedData.values
        .where((currentJob) => currentJob.jobId == '3')
        .firstOrNull;

I know formatting is hard, but is there a reason for that, I don’t see? Am I the only one or is there a silent majority who also dislike it? Is Bob around :smiley: ?

2 Likes

Try this: Settings → Editor → Code style → dart → Line Length: 120.
You can customize ‘Line Length’.

1 Like

I use 200, and it looks nice on my wide monitor

1 Like

That bothers me sooo much as well haha!

I cope with it sometimes by enforcing a block body instead of an expression.

 final job1 = someIndexedData.values.where((job) {
    return job.jobId == '3';
  }).firstOrNull;
2 Likes

Just one more idea to fix the formatting once the line length is increased is to use comment.

    final job3 = someIndexedData.values
        .where((currentJob) => currentJob.jobId == '3') //
        .firstOrNull;

If you don’t like the empty comment left, perhaps it’s a good way to greet your colleagues

3 Likes

Probably the best way to place otherwise useless characters in the code, Sir :smiley:

Thanks for the hint, but I don’t want longer lines. I prefer only one thought per line. My mind can’t process any more :face_with_spiral_eyes: