many_lints 0.4.0
many_lints: ^0.4.0 copied to clipboard
A comprehensive collection of custom lint rules, quick fixes, and code assists for Flutter and 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.4.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:
version: ^0.4.0
diagnostics:
prefer_center_over_align: true
use_bloc_suffix: false
Available Lints #
All 100 lints are enabled by default as warnings.
- always_remove_listener: Listener added but never removed in
dispose(). - avoid_accessing_collections_by_constant_index: Avoid accessing a collection by a constant index inside a loop.
- avoid_bloc_public_methods: Avoid declaring public members in Bloc classes. Use events via
addinstead. - avoid_border_all: Prefer
Border.fromBorderSideoverBorder.all. - 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_conditional_hooks: Warns when hooks are called inside conditional branches.
- avoid_constant_conditions: Both sides of a comparison are constants, so the result is always the same.
- avoid_constant_switches: The switch expression is a constant, so the result is always the same.
- avoid_contradictory_expressions: Detects contradictory comparisons in
&&chains that always evaluate to false. - avoid_duplicate_cascades: Detects duplicate cascade sections that indicate copy-paste errors.
- avoid_expanded_as_spacer: Prefer replacing
Expandedwith an empty child withSpacer. - avoid_flexible_outside_flex:
Flexible/Expandedshould only be used as a direct child ofRow,Column, orFlex. - avoid_generics_shadowing: Warns when a generic type parameter shadows a top-level declaration in the same file.
- avoid_incomplete_copy_with:
copyWithis missing constructor parameters. - avoid_incorrect_image_opacity: Use
Image'sopacityparameter instead of wrapping it in anOpacitywidget. - avoid_map_keys_contains: Use
containsKey()instead of.keys.contains(). - avoid_misused_test_matchers: Detects incompatible matcher usage with the actual value type.
- avoid_mounted_in_setstate: Checking
mountedinsidesetStateis too late and can lead to an exception. - avoid_notifier_constructors: Avoid constructors with logic in Notifier classes.
- avoid_only_rethrow: Catch clause contains only a rethrow statement.
- avoid_passing_bloc_to_bloc: Avoid passing a Bloc/Cubit to another Bloc/Cubit.
- avoid_passing_build_context_to_blocs: Avoid passing
BuildContextto a Bloc/Cubit. - avoid_public_notifier_properties: Avoid public properties on Notifier classes other than
state. - avoid_ref_inside_state_dispose: Avoid accessing
refinside thedispose()method. - avoid_ref_read_inside_build: Avoid using
ref.readinside thebuildmethod. - avoid_returning_widgets: Avoid returning widgets from functions, methods, or getters.
- avoid_shrink_wrap_in_lists: Avoid using
shrinkWrapinListView. - 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_single_field_destructuring: Avoid single-field destructuring. Use direct property access instead.
- avoid_state_constructors: Avoid constructors with logic in State classes.
- avoid_throw_in_catch_block: Avoid using
throwinside a catch block. - avoid_unassigned_stream_subscriptions: Stream subscription is not assigned to a variable.
- avoid_unnecessary_consumer_widgets: Ensures that a
ConsumerWidgetuses therefparameter. - avoid_unnecessary_gesture_detector: Detects
GestureDetectorwith no event handlers. - avoid_unnecessary_hook_widgets: Ensures that a
HookWidgetuses hooks. - avoid_unnecessary_overrides: Detects method overrides that only call
superwithout additional logic. - avoid_unnecessary_overrides_in_state: Detects State method overrides that only call
superwithout additional logic. - avoid_unnecessary_setstate: Detects unnecessary calls to
setState. - avoid_unnecessary_stateful_widgets: Detects
StatefulWidgetwith no mutable state. Consider usingStatelessWidget. - avoid_wrapping_in_padding: Avoid wrapping a widget in a
Paddingwidget when it already has padding support. - dispose_fields: Detects fields that are not disposed in
dispose(). - dispose_provided_instances: Detects instances with a dispose method that are not disposed via
ref.onDispose(). - 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_async_callback: Use
AsyncCallbackinstead ofFuture<void> Function(). - prefer_bloc_extensions: Use
context.read/context.watchinstead ofBlocProvider.of(). - prefer_center_over_align: Prefer
CenteroverAlignwithalignment: Alignment.center. - prefer_class_destructuring: Suggests using class destructuring for repeated property accesses.
- prefer_compute_over_isolate_run: Use
compute()instead ofIsolate.run()for web platform compatibility. - prefer_const_border_radius: Prefer
BorderRadius.all(Radius.circular())overBorderRadius.circular(). - prefer_constrained_box_over_container: Use
ConstrainedBoxinstead ofContainerwith only the constraints parameter. - prefer_container: Detects sequences of nested widgets that can be replaced with a single
Container. - prefer_contains: Use
.contains()instead of.indexOf()compared to-1. - prefer_correct_edge_insets_constructor: Use a simpler
EdgeInsetsconstructor. - prefer_enums_by_name: Use
.byName()instead of.firstWhere()to access enum values by name. - prefer_equatable_mixin: Prefer using
EquatableMixininstead of extendingEquatable. - prefer_expect_later: Prefer
expectLaterwhen testing Futures. - prefer_explicit_function_type: Prefer explicit return type and parameter list over bare
Functiontype. - prefer_for_loop_in_children: Prefer using a for-loop instead of functional list building.
- prefer_immutable_bloc_state: Bloc state classes should be annotated with
@immutable. - prefer_iterable_of: Prefer
.of()instead of.from()forList,Set, andMapfor compile-time type safety. - list_all_equatable_fields: Warns when an Equatable class does not list all instance fields in
props. - prefer_multi_bloc_provider: Prefer
MultiBlocProviderinstead of multiple nestedBlocProviders. - prefer_overriding_parent_equality: Parent class overrides
==andhashCodebut this class does not. - prefer_padding_over_container: Enforces the use of
PaddingoverContainerwith only margin. - prefer_return_await: Missing
awaiton returnedFutureinsidetry-catchblock. - 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_simpler_patterns_null_check: Prefer simpler null-check patterns in if-case expressions.
- prefer_single_setstate: Multiple
setStatecalls should be merged into a single call. - prefer_single_widget_per_file: Only one public widget per file.
- prefer_sized_box_square: Use
SizedBox.squareinstead ofSizedBoxwith equal width and height. - prefer_spacing: Prefer passing the
spacingargument instead of usingSizedBox. - prefer_switch_expression: Prefer switch expressions over switch statements when possible.
- prefer_test_matchers: Prefer using a
Matcherinstead of a literal value inexpect(). - prefer_text_rich: Use
Text.richinstead ofRichTextfor better text scaling and accessibility. - prefer_use_callback: Use
useCallbackinstead ofuseMemoizedfor memoizing functions. - prefer_transform_over_container: Use
Transforminstead ofContainerwith only the transform parameter. - prefer_type_over_var: Prefer an explicit type annotation over
var. - prefer_use_prefix: Custom hooks should start with the
useprefix. - prefer_void_callback: Use
VoidCallbackinstead ofvoid Function(). - prefer_wildcard_pattern: Use the wildcard pattern
_instead ofObject(). - proper_super_calls: Ensures
supercalls are placed correctly (first ininitState, last indispose). - use_bloc_suffix: Enforces the use of the
Blocsuffix for classes that extendBloc. - use_closest_build_context: Use the closest available
BuildContextinstead of the outer one. - 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_existing_destructuring: Use existing destructuring instead of accessing properties directly.
- use_existing_variable: Detects expressions that duplicate the initializer of an existing variable.
- use_gap: Prefer
Gapwidget instead ofSizedBoxorPaddingfor spacing in multi-child widgets. - use_notifier_suffix: Enforces the use of the
Notifiersuffix for classes that extendNotifier. - use_ref_and_state_synchronously: Don't use
reforstateafter an async gap without checkingref.mounted. - use_ref_read_synchronously: Avoid calling
ref.readafter an await point without checking if the widget is mounted. - use_sliver_prefix: Widget returns a sliver but its name does not start with
Sliver.
Quick fixes #
The following rules include auto-fixable quick fixes (78 total):
always_remove_listeneravoid_border_allavoid_cascade_after_if_nullavoid_commented_out_codeavoid_duplicate_cascadesavoid_expanded_as_spaceravoid_generics_shadowingavoid_incomplete_copy_withavoid_incorrect_image_opacityavoid_map_keys_containsavoid_notifier_constructorsavoid_only_rethrowavoid_ref_read_inside_buildavoid_single_field_destructuringavoid_state_constructorsavoid_throw_in_catch_blockavoid_unnecessary_consumer_widgetsavoid_unnecessary_gesture_detectoravoid_unnecessary_hook_widgetsavoid_unnecessary_overridesavoid_unnecessary_overrides_in_stateavoid_unnecessary_setstateavoid_unnecessary_stateful_widgetsavoid_wrapping_in_paddingdispose_fieldsdispose_provided_instancesprefer_abstract_final_static_classprefer_align_over_containerprefer_any_or_everyprefer_async_callbackprefer_bloc_extensionsprefer_center_over_alignprefer_class_destructuringprefer_compute_over_isolate_runprefer_const_border_radiusprefer_constrained_box_over_containerprefer_containerprefer_containsprefer_correct_edge_insets_constructorprefer_enums_by_nameprefer_equatable_mixinprefer_expect_laterprefer_explicit_function_typeprefer_for_loop_in_childrenprefer_immutable_bloc_stateprefer_iterable_oflist_all_equatable_fieldsprefer_multi_bloc_providerprefer_overriding_parent_equalityprefer_padding_over_containerprefer_return_awaitprefer_returning_shorthandsprefer_shorthands_with_constructorsprefer_shorthands_with_enumsprefer_shorthands_with_static_fieldsprefer_simpler_patterns_null_checkprefer_single_setstateprefer_sized_box_squareprefer_switch_expressionprefer_text_richprefer_use_callbackprefer_transform_over_containerprefer_type_over_varprefer_use_prefixprefer_void_callbackprefer_wildcard_patternproper_super_callsuse_bloc_suffixuse_closest_build_contextuse_cubit_suffixuse_dedicated_media_query_methodsuse_existing_destructuringuse_existing_variableuse_gapuse_notifier_suffixuse_ref_and_state_synchronouslyuse_ref_read_synchronouslyuse_sliver_prefix
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> {}
