avoid_global_mutable_state
Reports a top-level or static variable that is neither final nor const. Give the state an owner, or make it final.
- Tier
- advisory
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”A top-level or static variable that is neither final nor const:
int requestCount = 0;
String? lastError;
class Session { static Session? current;}All three are reported.
- A static variable is a static field of a class, a mixin, an enum, an extension or an extension type.
- How the variable is otherwise declared makes no difference: with
varor a type, with or without an initializer, withlateorexternal, public or private, in test code as elsewhere. - A variable that nothing assigns after its declaration is reported too: it only needs
final. - Each variable of one declaration is a violation of its own, at its name:
int first = 1, second = 2;has two.
It does not report:
- a
finalorconstvariable,final names = <String>[];,static const limit = 10;, or alate finalone; - an instance field or a local variable, which are no global state;
- a getter, a setter, a function or a static method.
A top-level or static variable that can be assigned is state the whole program shares. Any function can change it, so you cannot understand a function that reads it on its own: its result depends on what ran before. Tests that touch it pass or fail by the order they run in. And nothing in a signature says a function depends on it.
The cost comes later and elsewhere: a test that fails only in the full run, or a value that is stale after a hot reload.
State held by an object and handed to the code that needs it has an owner, a lifetime and a visible path. A value that never changes is no state at all: declare it final or const.
The rule is advisory because some global state is deliberate: a registry a framework expects, a cache for the lifetime of the process. The rule still shows it, so that it is a decision and not an accident.
Example
Section titled “Example”int requestCount = 0;
Future<Response> send(Request request) { requestCount++; return client.send(request);}class Transport { Transport(this.client);
final Client client;
int requestCount = 0;
Future<Response> send(Request request) { requestCount++; return client.send(request); }}A variable that is assigned once and never again only needs the keyword:
final defaultTimeout = Duration(seconds: 30);Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: avoid_global_mutable_state: exclude: ["tool/**"]Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression, which goes above the line of the variable’s name:
// The plugin registry of the host looks this variable up by name.// ignore: assay/avoid_global_mutable_state// ignore_for_file: assay/avoid_global_mutable_state silences it in a whole file, and // ignore: avoid_global_mutable_state without the prefix works too. See Suppressions.