avoid_long_files
This rule flags a file longer than the configured line budget.
Why use this rule
Section titled “Why use this rule”A long file is where a module stops having a single subject: the name at the top no longer describes everything below it, and finding the right place to add something becomes a scroll rather than a decision.
Blank lines and comments are not counted by default — a file is not hard to navigate because it is well documented. Lines are counted from the token stream, so a // inside a string literal is never mistaken for a comment.
Generated files are the obvious exception, and per-rule exclude handles them:
rules: avoid_long_files: exclude: ["**/*.g.dart", "**/*.freezed.dart"]This rule is in the pedantic preset: a budget is a house style, and the right number differs per codebase.
// user_page.dart — 800 lines holding the page, its three dialogs,// a formatter, and the API client it calls.// user_page.dart — the page// user_page_dialogs.dart — its dialogs// user_formatting.dart — the formatter// user_api.dart — the clientEnabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: avoid_long_files: enabled: trueOptions
Section titled “Options”many_lints: rules: avoid_long_files: max_lines: 300 count_comments: falserules: avoid_long_files: max_lines: 300 count_comments: false| Option | Type | Default | Description |
|---|---|---|---|
max_lines |
int | 300 |
How many code lines a file may hold |
count_comments |
bool | false |
Whether blank lines and comments count toward the budget |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: avoid_long_files: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_long_parameter_list— Keep parameter lists within a budget.avoid_too_many_methods— Keep a class within a method budget.max_imports— Keep a file within an import budget.avoid_long_functions— Keep function bodies within a line budget.