flutter_sane_lints 0.3.0 copy "flutter_sane_lints: ^0.3.0" to clipboard
flutter_sane_lints: ^0.3.0 copied to clipboard

A custom lint for flutter projects to avoid ad-hoc string literals.

Flutter Sane Lints #

style: very good analysis Powered by Mason License: MIT

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 💙

Installation 💻 #

❗ 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

Usage 🚀 #

Add the custom_lint package to your analysis_options.yaml:

analyzer:
  plugins:
    - custom_lint

Available rules #

At the moment this plugin contains only 2 rules:

  1. avoid_string_literals_inside_widget

This rule is equivalent to the Android's hard-coded string literals lint.

  1. 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.

Future work #

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.

4
likes
130
pub points
34%
popularity

Publisher

verified publisherel-darto.net

A custom lint for flutter projects to avoid ad-hoc string literals.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

analyzer, custom_lint_builder, meta

More

Packages that depend on flutter_sane_lints