Skip to content

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.

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 constant
if (themeMode == ThemeMode.dark) {
applyDarkStyle();
}
// LINT: negated comparison
final showSun = themeMode != ThemeMode.dark;
if (themeMode.isDark) {
applyDarkStyle();
}
final showSun = !themeMode.isDark;

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_theme_mode_getters: true

To turn it off again:

many_lints.yaml
rules:
prefer_theme_mode_getters: false

To keep the rule on but skip certain paths, use per-rule exclude.