Skip to content

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

The most common site is a build method branching on the app’s mode:

// Don't
@override
Widget build(BuildContext context) {
return Icon(
settings.themeMode == ThemeMode.dark ? Icons.dark_mode : Icons.light_mode,
);
}
// Do
@override
Widget build(BuildContext context) {
return Icon(
settings.themeMode.isDark ? Icons.dark_mode : Icons.light_mode,
);
}

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.

many_lints.yaml
rules:
prefer_theme_mode_getters: false

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