A Dart analyzer plugin that reports functions, methods, and constructors exceeding a configurable line count. Long functions are harder to read, test, and maintain — this rule helps keep them short.
The rule counts non-blank, non-comment lines inside function bodies (between { and }). It applies to:
- Top-level functions
- Instance and static methods
- Constructors
- Getters and setters
- Anonymous functions (lambdas/closures)
Expression-bodied functions (=> expr) are always skipped since they are inherently concise.
Only lines between the opening { and closing } are counted. The following are excluded:
- Blank lines
- Lines containing only comments (
//,/* ... */,* ...)
This means well-commented code is not penalized.
Add the plugin to your analysis_options.yaml:
plugins:
avoid_long_functions:
path: <path_to_this_package>
diagnostics:
avoid_long_functions: warningThe severity can be set to warning, error, or info.
The maximum line count defaults to 20. Override it with the max_lines option:
plugins:
avoid_long_functions:
path: <path_to_this_package>
max_lines: 30
diagnostics:
avoid_long_functions: warningGiven max_lines: 20, this function would trigger the rule:
void processData(List<int> data) {
// ... 21+ non-blank, non-comment lines ...
}The diagnostic message looks like:
The function 'processData' is 25 lines long (maximum is 20).
Try refactoring into smaller functions.
Use standard Dart ignore comments:
// ignore: avoid_long_functions
void legacyFunction() {
// ...
}Or for an entire file:
// ignore_for_file: avoid_long_functions- Dart SDK
^3.11.0