A custom lint plugin to try and improve maintainability in Flutter projects by disallowing certain anti-patterns. At the moment, the only dart lint rule in this plugin is to avoid declaring ad-hoc strings inside Widget/State classes, or passing a literal to a Widget constructor.
Every String used for presentation (such as in a Text widget) should be defined in a l10n file. See Flutter Internationalization Tutorial.
Every String used for logic (such as in a switch statement) should be defined as an Enum or in any other way that makes sense for the use case while following good OO design. Using strings is a clear sign of primitive obsession and should be avoided.
Created with the Very Good CLI 💙
❗ In order to start using Flutter Sane Lints you must have the Flutter SDK installed on your machine.
Add flutter_sane_lints
and custom_lint
to your pubspec.yaml
:
dev_dependencies:
custom_lint:
flutter_sane_lints:
Install it:
flutter pub get
Add the custom_lint package to your analysis_options.yaml
:
analyzer:
plugins:
- custom_lint
At the moment this plugin contains only 2 rules:
- avoid_string_literals_inside_widget
This rule is equivalent to the Android's hard-coded string literals lint.
- avoid_if_with_enum
This rule is made to be used alongside the existing exhaustive_cases rule, as part of the default rules.
The existing Dart rule ensures you cover all enum cases in your switch statement, which is great for future-proofing your app. However, it does not raise a warning if you are using an if statement with your enum. This is bad, because using if with enum is the same as using a switch with default:
if (myValue == MyEnum.firstOption) {
doSomething();
}
is the same as
switch (myValue) {
case MyEnum.firstOption:
doSomething();
default:
// this default case violates exhaustive_cases
}
Therefore, using if statements with enum values violate Dart's rule of performing exhaustive switch.
More rules will be added in the future, but in the meantime, I am open to requests. Please go to the issues page and request for further features.