prefer_theme_mode_getters
v0.7.0 Warning Fix Widget Best Practices
Warns when a ThemeMode value is compared with == or != against a ThemeMode constant. Flutter 3.44 added dedicated getters — isDark, isLight, and isSystem — that express the same check more directly.
Why use this rule
Section titled “Why use this rule”mode.isDark reads as intent rather than mechanics and matches the direction of the Flutter API. The getters also make call sites shorter and remove the duplicated ThemeMode. noise from conditions.
The rule only reports when the resolved ThemeMode enum actually declares the getter, so it stays silent on projects using Flutter older than 3.44 and the quick fix can never produce non-compiling code.
See also: Flutter 3.44.0 release notes
// LINT: compares against the enum constantif (themeMode == ThemeMode.dark) { applyDarkStyle();}
// LINT: negated comparisonfinal showSun = themeMode != ThemeMode.dark;if (themeMode.isDark) { applyDarkStyle();}
final showSun = !themeMode.isDark;Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_theme_mode_getters: trueTo turn it off again:
rules: prefer_theme_mode_getters: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”always_pass_global_key— Don’t create a GlobalKey inside build.avoid_conditional_hooks— Never call hooks inside conditionals, loops, or ternaries.avoid_deep_widget_nesting— Keep a widget tree within a nesting budget.avoid_flexible_outside_flex— Only use Flexible and Expanded as direct children of Row, Column, or Flex.