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.
With max_lines: 10, one file carrying the model, its formatting and its
transport:
class Invoice { const Invoice(this.id, this.cents); final String id; final int cents;}
String formatInvoice(Invoice invoice) { final major = invoice.cents ~/ 100; final minor = invoice.cents % 100; return '\$$major.${minor.toString().padLeft(2, '0')}';}
Future<Invoice> fetchInvoice(String id) async { return Invoice(id, 0);}Split on the subjects the file already has — invoice.dart,
invoice_formatting.dart, invoice_api.dart:
class Invoice { const Invoice(this.id, this.cents); final String id; final int cents;}String formatInvoice(Invoice invoice) { final major = invoice.cents ~/ 100; final minor = invoice.cents % 100; return '\$$major.${minor.toString().padLeft(2, '0')}';}Future<Invoice> fetchInvoice(String id) async { return Invoice(id, 0);}Enabling 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.