avoid-global-state
Same as avoid-global-state in Dart Code Metrics
- Group
- Correctness
- Path
lint/correctness/avoid-global-state- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 67 of 512
Disallow mutable global and static state.
Flags top-level variables and static fields that are mutable — anything not
declared const or plain final, including late final, which can still be
assigned once at an arbitrary point. Mutable global state is reachable and
writable from anywhere, which makes data flow hard to trace, introduces
initialization-order and concurrency hazards, and lets tests contaminate one
another through leftover shared state. Prefer const or final
declarations, and thread mutable state explicitly through constructors or
parameters rather than hanging it off globals.
Invalid
// Bad: mutable global state
var globalList = <String>[];
int requestCount = 0;
String? cachedValue;
Map<String, dynamic> config = {};
class Database {
static var instance;
}
List<int> shared = [];
void incrementCounter() {
requestCount++; // uses bad global
}
late final String lateGlobal;
var uninitializedGlobal;// Bad: mutable global statevar globalList = <String>[];var globalList = <String>[];int requestCount = 0;int requestCount = 0;String? cachedValue; Map<String, dynamic> config = {};class Database { static var instance; List<int> shared = []; late final String lateGlobal;late final String lateGlobal;var uninitializedGlobal;Valid
// Good: const or immutable state
const String appName = 'MyApp';
const int maxRetries = 3;
const List<String> allowedRoles = ['admin', 'user'];
final Map<String, dynamic> config = {'version': 1};
final Duration defaultTimeout = Duration(seconds: 30);
// Good: state inside a class
class DatabaseManager {
static final DatabaseManager _instance = DatabaseManager._();
factory DatabaseManager() => _instance;
DatabaseManager._();
}
class Counter {
int count = 0;
void increment() {
count++;
}
}
// Good: function-local state
void processRequest() {
final List<String> tempList = [];
var count = 0;
// local variables are OK
}
// OK: @memoized pattern
class Service {
@memoized
Future<String> fetchData() async {
return 'data';
}
}Configure avoid-global-state
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-global-state": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-global-state: <reason>More correctness rules
- avoid-hooks-outside-buildrecommendedflutter_hooks
Flags confirmed Flutter hooks outside hook builds, custom hooks, and HookBuilder/HookConsumer builders.
- avoid-incorrect-image-opacityflutter
Flags Opacity directly wrapping a Flutter Image, including named constructors.
- avoid-inherited-widget-in-initstaterecommendedflutter
Avoid inherited-widget subscriptions during initState.
- avoid-instantiating-in-value-providerprovider
Value providers borrow existing instances; newly constructed values need create.
- avoid-late-contextrecommendedflutter
Avoid storing
BuildContextin late fields or reading it in late initializers. - avoid-missing-completer-stack-trace
Preserve error origins when calling SDK
Completer.completeError. - avoid-missing-controllerflutter
Require Flutter text fields to expose their updated value.
- avoid-missing-enum-constant-in-map
Require enum-keyed map literals to include every declared enum value.