概要
Flutter 2.5からflutter create
でプロジェクトを作成した際、デフォルトでlinterが搭載されるようになりました。
公式ページに2.5以前で作成されたプロジェクト用のマイグレーションガイドも掲載されています。
備忘録も兼ねて、どのようなLintが有効になったのか確認します。
Lint一覧
元ソースコード
Flutterプロジェクトではデフォルトで3つのrulesetsが適用されます。
flutter rulesetsがrecommendedを、recommendedがcoreをincludeしているという形になるため、上記3つが適用されます。
標準搭載されているlint (2021/9/30時点)
flutter
error
- avoid_print (productionコード内でのprint関数の使用禁止)
- avoid_web_libraries_in_flutter (Webプラグイン以外でのWebライブラリの使用禁止)
- no_logic_in_create_state (createState関数内でロジックを書くことの禁止)
- use_key_in_widget_constructors (Widgetのコンストラクタに必ずkeyを入れる)
style
- avoid_unnecessary_containers (不要なContainerでのWrapを検出)
- prefer_const_constructors (constでコンストラクタを呼べる場合はconstを使う)
- prefer_const_constructors_in_immutables (immutableなクラスのコンストラクタにはconstを使う)
- prefer_const_declarations (const宣言が使える時は使う)
- prefer_const_literals_to_create_immutables (immutableなクラスの初期値設定時にconstを使う)
- sized_box_for_whitespace (空白を作る場合にContainerではなくSizedBoxを使う)
- use_full_hex_values_for_flutter_colors (色は8桁の16進数で指定する)
recommended
Dartの基本的な書き方になるので一覧だけ載せます。
- always_require_non_null_named_parameters
- annotate_overrides
- avoid_function_literals_in_foreach_calls
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_renaming_method_parameters
- avoid_return_types_on_setters
- avoid_returning_null_for_void
- avoid_single_cascade_in_expression_statements
- constant_identifier_names
- control_flow_in_finally
- empty_constructor_bodies
- empty_statements
- exhaustive_cases
- implementation_imports
- library_names
- library_prefixes
- null_closures
- overridden_fields
- package_names
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_contains
- prefer_equal_for_default_values
- prefer_final_fields
- prefer_for_elements_to_map_fromIterable
- prefer_function_declarations_over_variables
- prefer_if_null_operators
- prefer_initializing_formals
- prefer_inlined_adds
- prefer_is_not_operator
- prefer_null_aware_operators
- prefer_spread_collections
- prefer_void_to_null
- recursive_getters
- slash_for_doc_comments
- type_init_formals
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_getters_setters
- unnecessary_new
- unnecessary_null_in_if_null_operators
- unnecessary_string_escapes
- unnecessary_string_interpolations
- unnecessary_this
- use_function_type_syntax_for_parameters
- use_rethrow_when_possible
core
こちらも一覧だけ載せます。
- avoid_empty_else
- avoid_relative_lib_imports
- avoid_shadowing_type_parameters
- avoid_types_as_parameter_names
- await_only_futures
- camel_case_extensions
- camel_case_types
- curly_braces_in_flow_control_structures
- empty_catches
- file_names
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- no_duplicate_case_values
- non_constant_identifier_names
- package_prefixed_library_names
- prefer_generic_function_type_aliases
- prefer_is_empty
- prefer_is_not_empty
- prefer_iterable_whereType
- prefer_typing_uninitialized_variables
- provide_deprecation_message
- unnecessary_overrides
- unrelated_type_equality_checks
- valid_regexps
- void_checks
その他気になったrule
特にerror関連で、使ってもよさそうなものを抜粋。
- always_use_package_imports (相対パスでのimportを全面禁止。avoid_relative_lib_importsの強化版)
- avoid_dynamic_calls (dynamicオブジェクトのcall、アクセスを禁止)
- avoid_type_to_string (productionで
runtimeType.toString()
は省略される可能性があるので禁止) - literal_only_boolean_expressions (定数値のみのif文を禁止)
- invariant_booleans (if文で同じ条件を使用しているものを抽出) ※現状experimental
- no_adjacent_strings_in_list (List内で文字の隣接を禁止、コンマ忘れ防止)
- prefer_relative_imports (絶対パスでのimportを禁止)
- unnecessary_statements (不要な行を検出)
- use_build_context_synchronously (async関数にBuildContextを渡すのを禁止) ※現状experimental
所感
- IDEで問題のある箇所を掲示してくれるので、修正がしやすくなった
- 特にconst、Container系のパフォーマンス最適化に役立つ
- プロジェクト内で表記の統一をさせることができる
- パッケージのパス指定など