Skip to content

prefer_prefixed_global_constants

v1.0.0WarningConfigurableClass Naming

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.

analysis_options.yaml
many_lints:
rules:
prefer_prefixed_global_constants:
prefix: k

With prefix: k:

const defaultTimeout = Duration(seconds: 30); // LINT
const maxRetries = 3; // LINT
const serviceHost = 'example.com'; // LINT
const kDefaultTimeout = Duration(seconds: 30);
const kMaxRetries = 3;
const kServiceHost = 'example.com';

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 diagnostics
const minPort = 1024, maxPort = 65535;
// Do
const 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 library
const _retryBackoff = Duration(milliseconds: 200);
// Not reported — `final`, not `const`
final sessionId = generateId();
// Reported
const timeout = Duration(seconds: 5); // LINT

The prefix is a plain string, so a project with its own vocabulary spells it out:

rules:
prefer_prefixed_global_constants:
prefix: app
// Don't
const primaryColorHex = 0xFF0468D7; // LINT
// Do
const appPrimaryColorHex = 0xFF0468D7;

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']
Option Type Default Description
prefix string — The required prefix. Unset means the rule is silent

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.

To disable this rule:

many_lints.yaml
rules:
prefer_prefixed_global_constants: false

To keep the rule on but skip certain paths, use per-rule exclude.