はじめに
こんにちは、エンジニアのkeitaMaxです。
前回の記事
Flutterアプリの静的解析を行なっていきたいと思います。
インストール
Flutterプロジェクトを作成したら初期で入っているようなのでインストールはいらないです。
解析
flutter analyzeコマンドを実行すると静的解析ができます。
flutter_example_app % flutter analyze
Resolving dependencies...
Downloading packages...
characters 1.4.0 (1.4.1 available)
flutter_lints 5.0.0 (6.0.0 available)
go_router 10.2.0 (16.3.0 available)
lints 5.1.1 (6.0.0 available)
material_color_utilities 0.11.1 (0.13.0 available)
meta 1.16.0 (1.17.0 available)
test_api 0.7.6 (0.7.7 available)
Got dependencies!
7 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Analyzing flutter_example_app...
No issues found! (ran in 1.1s)
失敗させてみる
const TopPage({super.key});
を以下のように;をなくしてエラーを出して見ます。
const TopPage({super.key})
これでflutter analyzeを実行してみると
flutter_example_app % flutter analyze
Resolving dependencies...
Downloading packages...
characters 1.4.0 (1.4.1 available)
flutter_lints 5.0.0 (6.0.0 available)
go_router 10.2.0 (16.3.0 available)
lints 5.1.1 (6.0.0 available)
material_color_utilities 0.11.1 (0.13.0 available)
meta 1.16.0 (1.17.0 available)
test_api 0.7.6 (0.7.7 available)
Got dependencies!
7 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Analyzing flutter_example_app...
error • Expected to find ';' • lib/features/top/presentation/pages/top_page.dart:15:35 • expected_token
1 issue found. (ran in 1.0s)
としっかりエラーが出るようになりました。
Linterルールの追加
analysis_options.yamlにルールを書くことでルールを設定することができます。
ダブルクオートをNGにしてシングルクオートのみにするルールを追加して見ましょう。
まずは適当なファイルに以下のようにtextという変数に"test"を入れて見ます。
static const text = "test";
この状態でflutter analyzeを実行してみるとエラーが出ないと思います。
次にanalysis_options.yamlにルールを追加します。
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
prefer_single_quotes: true
prefer_single_quotes: trueをrulesの中に追加してflutter analyzeを実行して見ましょう。
flutter_example_app % flutter analyze
Resolving dependencies...
Downloading packages...
characters 1.4.0 (1.4.1 available)
flutter_lints 5.0.0 (6.0.0 available)
go_router 10.2.0 (16.3.0 available)
lints 5.1.1 (6.0.0 available)
material_color_utilities 0.11.1 (0.13.0 available)
meta 1.16.0 (1.17.0 available)
test_api 0.7.6 (0.7.7 available)
Got dependencies!
7 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Analyzing flutter_example_app...
info • Unnecessary use of double quotes • lib/features/top/presentation/pages/top_page.dart:6:23 • prefer_single_quotes
1 issue found. (ran in 1.1s)
このようにエラーが出ることが確認できました。
自動修正
dart fix --applyというコマンドを使うことで自動修正できるらしいです。
実際に実行してみると
flutter_example_app % dart fix --apply
Computing fixes in flutter_example_app... 1.7s
Applying fixes... 0.0s
lib/features/top/presentation/pages/top_page.dart
prefer_single_quotes • 1 fix
1 fix made in 1 file.
となり、修正されたようです。
static const text = 'test';
と実際に修正されていました。
念の為flutter analyzeで確認してみると、
flutter_example_app % flutter analyze
Resolving dependencies...
Downloading packages...
characters 1.4.0 (1.4.1 available)
flutter_lints 5.0.0 (6.0.0 available)
go_router 10.2.0 (16.3.0 available)
lints 5.1.1 (6.0.0 available)
material_color_utilities 0.11.1 (0.13.0 available)
meta 1.16.0 (1.17.0 available)
test_api 0.7.6 (0.7.7 available)
Got dependencies!
7 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Analyzing flutter_example_app...
No issues found! (ran in 0.7s)
エラーが亡くなっていることが確認できました。
おわりに
この記事での質問や、間違っている、もっといい方法があるといったご意見などありましたらご指摘していただけると幸いです。
最後まで読んでいただきありがとうございました!
参考
次の記事