many_lints 0.3.0
many_lints: ^0.3.0 copied to clipboard
A useful collection of custom lints for Flutter & Dart projects
A useful collection of custom lints for Flutter & Dart projects. Uses the new analysis_server_plugin system for direct integration with dart analyze and IDEs.
Features #
This package provides a set of custom lints to help you write better Flutter code.
Getting started #
Requires Dart 3.10+ (Flutter 3.38+)
Add many_lints to the top-level plugins section in your analysis_options.yaml file (NOT under analyzer:):
plugins:
many_lints: ^0.3.0
That's it — the analysis server will automatically download and resolve the plugin from pub.dev. There is no need to add it to your pubspec.yaml.
Local development #
For local development or when using many_lints from a cloned repository, use the path option:
git clone https://github.com/Nikoro/many_lints.git /path/to/many_lints
plugins:
many_lints:
path: /path/to/many_lints
Note: Git dependencies are not directly supported by the plugin system. Clone the repository locally and use the
pathoption instead.
Important: After any change to the
pluginssection, you must restart the Dart Analysis Server.
Configuring diagnostics #
All rules are registered as warnings and enabled by default. You can enable or disable individual rules under the diagnostics key:
plugins:
many_lints:
diagnostics:
prefer_center_over_align: true
use_bloc_suffix: false
Available Lints #
All lints are enabled by default as warnings.
- avoid_accessing_collections_by_constant_index: Avoid accessing a collection by a constant index inside a loop.
- avoid_cascade_after_if_null: Cascade after if-null operator without parentheses can produce unexpected results.
- avoid_collection_equality_checks: Comparing collections with
==/!=checks reference equality, not contents. - avoid_collection_methods_with_unrelated_types: Detects collection method calls with arguments unrelated to the collection's type.
- avoid_commented_out_code: Detects comments that look like commented-out code.
- avoid_single_child_in_multi_child_widgets: Avoid using a single child in widgets that can accept multiple children (e.g.,
Row,Column,Flex). - avoid_unnecessary_consumer_widgets: Ensures that a
ConsumerWidgetuses therefparameter. - avoid_unnecessary_hook_widgets: Ensures that a
HookWidgetuses hooks. - prefer_abstract_final_static_class: Classes with only static members should be declared as
abstract final. - prefer_align_over_container: Enforces the use of
AlignoverContainerwith only the alignment parameter. - prefer_any_or_every: Prefer
.any()or.every()over.where().isEmpty/.isNotEmpty. - prefer_center_over_align: Prefer
CenteroverAlignwithalignment: Alignment.center. - prefer_explicit_function_type: Prefer explicit return type and parameter list over bare
Functiontype. - prefer_iterable_of: Prefer
.of()instead of.from()forList,Set, andMapfor compile-time type safety. - prefer_padding_over_container: Enforces the use of
PaddingoverContainerwith only margin. - prefer_returning_shorthands: Prefer dot shorthands when the instance type matches the return type.
- prefer_shorthands_with_constructors: Prefer dot shorthands instead of explicit class instantiations.
- prefer_shorthands_with_enums: Prefer dot shorthands instead of explicit enum prefixes.
- prefer_shorthands_with_static_fields: Prefer dot shorthands instead of explicit class prefixes for static fields.
- prefer_switch_expression: Prefer switch expressions over switch statements when possible.
- prefer_type_over_var: Prefer an explicit type annotation over
var. - use_bloc_suffix: Enforces the use of the
Blocsuffix for classes that extendBloc. - use_cubit_suffix: Enforces the use of the
Cubitsuffix for classes that extendCubit. - use_dedicated_media_query_methods: Enforces the use of dedicated
MediaQuerymethods instead ofMediaQuery.of(context). - use_gap: Prefer
Gapwidget instead ofSizedBoxorPaddingfor spacing in multi-child widgets. - use_notifier_suffix: Enforces the use of the
Notifiersuffix for classes that extendNotifier.
Quick fixes #
The following rules include auto-fixable quick fixes:
avoid_cascade_after_if_nullavoid_commented_out_codeavoid_unnecessary_consumer_widgetsavoid_unnecessary_hook_widgetsprefer_abstract_final_static_classprefer_align_over_containerprefer_any_or_everyprefer_center_over_alignprefer_explicit_function_typeprefer_iterable_ofprefer_padding_over_containerprefer_returning_shorthandsprefer_shorthands_with_constructorsprefer_shorthands_with_enumsprefer_shorthands_with_static_fieldsprefer_switch_expressionprefer_type_over_varuse_bloc_suffixuse_cubit_suffixuse_dedicated_media_query_methodsuse_gapuse_notifier_suffix
Available Assists #
- Convert to collection-for: Converts
.map().toList()or.map().toSet()to collection-for syntax.
Suppressing Diagnostics #
To suppress a specific lint, use comments:
// ignore: many_lints/prefer_center_over_align
const Align(...);
// ignore_for_file: many_lints/use_bloc_suffix
Example #
See the example/ directory for a Flutter project that demonstrates every lint rule in action. Each file corresponds to a single rule and contains code that triggers the lint.
use_cubit_suffix #
DO use Cubit suffix for your cubit names.
BAD:
class MyClass extends Cubit<bool> {}
GOOD:
class MyClassCubit extends Cubit<bool> {}
