8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutterの`flutter format`コマンドのオプションTips

Posted at

例えば、以下のように余分なスペースが入ってしまっているコードがあるとします。

lib/hello.dart
Text( 'Hello world')

flutterではflutter format と言うformatを自動で整形してくれるコマンドが用意されているのでこれを使います。

$ flutter format lib
Formatted lib/hello.dart
Formatted 1 files (1 changed) in 0.45 seconds.

以下、flutter formatを扱う際に便利なオプションについて記述します。

formatエラーの際にExit code 1を返したい

CIでformat違反が発生した場合にCI全体をfailさせたい場合など、exit code 1を 返却したいケースがあります。
その場合--set-exit-if-changedオプションを追加することで設定可能です。

$ flutter format lib --set-exit-if-changed 
Formatted lib/hello.dart
Formatted 1 files (1 changed) in 0.45 seconds.
Formatting failed: 1

コードの更新まで行わずにformat違反かだけチェックしたい

コードの自動整形は行わずに違反が存在するかのチェックだけで充分なケースがあります。
その場合--dry-runオプションを追加することで設定可能です。

$ flutter format lib --set-exit-if-changed --dry-run
Formatted lib/hello.dart
Formatted 1 files (1 changed) in 0.45 seconds.
Formatting failed: 1

環境

  • Flutter 2.10.4
  • Dart SDK version: 2.16.2
8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?