avoid-top-level-member-access
- Group
- Style
- Path
lint/style/avoid-top-level-member-access- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 146 of 512
Flags mutable global state: non-const, non-final top-level variables and
static fields, plus every read of mutable top-level variables.
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.
Invalid
// 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;
}
} var globalCounter = 0; int globalState = 5; List<String> globalList = []; Map<String, dynamic> globalMap = {}; String? globalString;void incrementCounter() { globalCounter++; globalCounter++; globalCounter += 1;void useGlobalState() { print(globalState); print(globalState); globalState = 10;void manipulateGlobalList() { globalList.add('item'); globalList.add('item'); globalList.clear();int getGlobalValue() { return globalState * 2;class Singleton { static int instances = 0; bool isInitialized = false;void checkInitialization() { if (!isInitialized) { if (!isInitialized) { isInitialized = true;Valid
// 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,
}Configure avoid-top-level-member-access
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-top-level-member-access": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-top-level-member-access: <reason>More style rules
- avoid-types-on-closure-parameters
avoid_types_on_closure_parametersFlags type annotations on closure parameters whose types come from context.
- avoid-unnecessary-block
Flags standalone nested blocks that introduce no lexical declarations.
- avoid-unnecessary-call
Prefer direct invocation of functions and resolved callable instances.
- avoid-unnecessary-continue
Flags an unlabeled continue at the end of a loop body or its final branch.
- avoid-unnecessary-getter
Flags a getter that only exposes a private final field.
- avoid-unnecessary-if
Flags an if branch that only returns the same expression as the following return.
- avoid-unnecessary-length-check
Empty collections already skip
for-inandforEachbodies. - avoid-unnecessary-local-variable
Reports a single-use local used only to initialize the next local.