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

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.
5
6int globalState = 5;
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,
}

Configure avoid-top-level-member-access

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-top-level-member-access": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-top-level-member-access: <reason>