🐦
Dart 3.8にアップデートしたらFlutterのフォーマットがおかしくなった
問題
Dartを3.8にアップデートしたところ、VSCodeでセーブした際の自動整形機能で、末尾のカンマが削除されるようになってしまった。
これでセーブすると、
body: ListView(
children: [ListTile(leading: Icon(Icons.home), title: Text('Home'),),],
),
カンマが消えてしまう。
body: ListView(
children: [ListTile(leading: Icon(Icons.home), title: Text('Home'))],
),
以前は、カンマがついている時はいい感じにインデントを入れて整形してくれていたのに。
原因
Dart 3.8で新しいフォーマット設定になったのが原因。
新しいフォーマッタでは構文の長さによっては、末尾のカンマを消す仕様らしい。
Trailing commas and taller code
In prior releases, a trailing comma would force the surrounding construct to split. The new formatter now decides whether a construct should split and then adds or removes the trailing comma as needed.
対策
カンマを保持するオプションを有効にすればOK。
Flutterプロジェクト内のanalysis_options.yaml
に以下を追加する。
formatter:
trailing_commas: preserve
すっきり
見慣れたフォーマットスタイルになって安心
body: ListView(
children: [
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
),
],
),
Discussion