Rules / Style / avoid-top-level-member-access
avoid-top-level-member-access
lint/style/avoid-top-level-member-accessrecommended
Source: FalconFlags mutable global state: non-const, non-final top-level variables and
static fields, plus every read of them.
Mutable top-level and static members are shared, unscoped state that any
code can read or write, which makes data flow hard to follow and tests hard
to isolate. Prefer const or final values, or pass state explicitly
through constructors and parameters. The rule reports each offending
declaration and every reference to a mutable top-level variable from within
a function body.
Invalid
example.dartdart
// Test cases for avoid-top-level-member-access rule
// All lines with violations should have an expectation annotation
var globalCounter = 0;
int globalState = 5;
List<String> globalList = [];
Map<String, dynamic> globalMap = {};
String? globalString;
void incrementCounter() {
globalCounter++;
globalCounter += 1;
}
void useGlobalState() {
print(globalState);
globalState = 10;
}
void manipulateGlobalList() {
globalList.add('item');
globalList.clear();
}
int getGlobalValue() {
return globalState * 2;
}
class Singleton {
static int instances = 0;
}
bool isInitialized = false;
void checkInitialization() {
if (!isInitialized) {
isInitialized = true;
}
}
Avoid using non-const top-level members
3
4var globalCounter = 0;
∙
Avoid using non-const top-level members
Avoid using non-const top-level members
7
8List<String> globalList = [];
∙
Avoid using non-const top-level members
9
10Map<String, dynamic> globalMap = {};
∙
Avoid using non-const top-level members
11
12String? globalString;
∙
Avoid using non-const top-level members
14void incrementCounter() {
15 globalCounter++;
∙
Avoid using non-const top-level members
15 globalCounter++;
16 globalCounter += 1;
∙
Avoid using non-const top-level members
19void useGlobalState() {
20 print(globalState);
∙
Avoid using non-const top-level members
20 print(globalState);
21 globalState = 10;
∙
Avoid using non-const top-level members
24void manipulateGlobalList() {
25 globalList.add('item');
∙
Avoid using non-const top-level members
25 globalList.add('item');
26 globalList.clear();
∙
Avoid using non-const top-level members
29int getGlobalValue() {
30 return globalState * 2;
∙
Avoid using non-const top-level members
33class Singleton {
34 static int instances = 0;
∙
Avoid using non-const top-level members
36
37bool isInitialized = false;
∙
Avoid using non-const top-level members
39void checkInitialization() {
40 if (!isInitialized) {
∙
Avoid using non-const top-level members
40 if (!isInitialized) {
41 isInitialized = true;
∙
Valid
example.dartdart
// Good cases for avoid-top-level-member-access rule
// No violations expected
const kTimeout = 30;
const kMaxRetries = 3;
final kDefaultConfig = <String, dynamic>{};
class Counter {
int _value = 0;
void increment() {
_value++;
}
int get value => _value;
}
class GlobalState {
static final _instance = GlobalState._();
factory GlobalState() => _instance;
GlobalState._();
int _state = 5;
void setState(int newValue) {
_state = newValue;
}
}
abstract class Repository {
void addItem(String item);
List<String> getItems();
}
class InMemoryRepository implements Repository {
final _items = <String>[];
@override
void addItem(String item) {
_items.add(item);
}
@override
List<String> getItems() => _items;
}
class Config {
static const String appName = 'MyApp';
static const String version = '1.0.0';
}
const String baseUrl = 'https://api.example.com';
final RegExp emailRegex = RegExp(r'^[^@]+@[^@]+\.[^@]+$');
class Logger {
static void log(String message) {
print('[${DateTime.now()}] $message');
}
}
enum Status {
pending,
active,
completed,
}
How to configure
Set the severity of avoid-top-level-member-access in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"avoid-top-level-member-access": "error"
}
}
}
}