prefer_theme_mode_getters
v0.7.0 Warning Fix Widget Best Practices
Flags a ThemeMode compared with == or != against ThemeMode.dark, ThemeMode.light or ThemeMode.system. Flutter 3.44 added isDark, isLight and isSystem, which say the same thing without the repeated ThemeMode. noise. The quick fix rewrites the comparison.
This rule is in the opinionated preset, so it is on with preset: opinionated and preset: pedantic. No configuration.
See also: Flutter 3.44.0 release notes
final themeMode = ThemeMode.dark;
// LINT: compares against the enum constantif (themeMode == ThemeMode.dark) { applyDarkStyle();}
// LINT: negated comparisonfinal showSun = themeMode != ThemeMode.dark;final themeMode = ThemeMode.dark;
if (themeMode.isDark) { applyDarkStyle();}
final showSun = !themeMode.isDark;The constant can be on either side
Section titled “The constant can be on either side”// Don'tfinal isLight = ThemeMode.light == settings.themeMode;
// Dofinal isLight = settings.themeMode.isLight;In a widget
Section titled “In a widget”The most common site is a build method branching on the app’s mode:
// Don't@overrideWidget build(BuildContext context) { return Icon( settings.themeMode == ThemeMode.dark ? Icons.dark_mode : Icons.light_mode, );}
// Do@overrideWidget build(BuildContext context) { return Icon( settings.themeMode.isDark ? Icons.dark_mode : Icons.light_mode, );}Known limitations
Section titled “Known limitations”Nothing is reported before Flutter 3.44. The rule checks that the resolved ThemeMode enum actually declares the getter, so an older Flutter sees no diagnostics and the quick fix can never produce non-compiling code.
A switch on ThemeMode is not reported — the getters do not replace exhaustive matching, and a switch is usually the better shape when all three cases matter.
Turning this rule off
Section titled “Turning this rule off”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.