prefer_prefixed_global_constants
Flags a public top-level constant that does not carry the prefix you configure.
A top-level constant is in scope everywhere the library is imported, under a name that competes with every local. defaultTimeout at the top level reads identically to a local of the same name at the point of use, so a prefix — kDefaultTimeout, Flutter’s own convention — makes the origin visible without a jump to the declaration.
This rule is in no preset and reports nothing until you set prefix:. Every example below shows the configuration that produces it.
many_lints: rules: prefer_prefixed_global_constants: prefix: krules: prefer_prefixed_global_constants: prefix: kWith prefix: k:
const defaultTimeout = Duration(seconds: 30); // LINTconst maxRetries = 3; // LINTconst serviceHost = 'example.com'; // LINTconst kDefaultTimeout = Duration(seconds: 30);const kMaxRetries = 3;const kServiceHost = 'example.com';Examples
Section titled “Examples”Every declaration in one statement is checked
Section titled “Every declaration in one statement is checked”rules: prefer_prefixed_global_constants: prefix: k// Don't — two constants, two diagnosticsconst minPort = 1024, maxPort = 65535;
// Doconst kMinPort = 1024, kMaxPort = 65535;Private constants and non-constants are left alone
Section titled “Private constants and non-constants are left alone”// Not reported — private, so it cannot collide outside this libraryconst _retryBackoff = Duration(milliseconds: 200);
// Not reported — `final`, not `const`final sessionId = generateId();
// Reportedconst timeout = Duration(seconds: 5); // LINTA different house prefix
Section titled “A different house prefix”The prefix is a plain string, so a project with its own vocabulary spells it out:
rules: prefer_prefixed_global_constants: prefix: app// Don'tconst primaryColorHex = 0xFF0468D7; // LINT
// Doconst appPrimaryColorHex = 0xFF0468D7;Scope it to one layer
Section titled “Scope it to one layer”Design tokens may want the prefix while generated code does not:
rules: prefer_prefixed_global_constants: prefix: k exclude: ['lib/**/*.g.dart', 'lib/**/*.freezed.dart']Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
prefix |
string | — |
The required prefix. Unset means the rule is silent |
Known limitations
Section titled “Known limitations”Constants only. A top-level final or var is not reported, even though it shares the same scope problem. Only const declarations are checked.
Case is not enforced. The check is a plain startsWith, so ktimeout satisfies prefix: k as readily as kTimeout does. Pair this with prefer_correct_type_name and the SDK’s constant_identifier_names if casing matters.
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_prefixed_global_constants: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_unnecessary_enum_prefix— Drop an enum name repeated in its own constants.match_class_name_pattern— Match class names against a regular expression.prefer_boolean_prefixes— Name booleans as questions.prefer_correct_callback_field_name— Name callbacks onSomething, the way Flutter does.