Rithvik Nishad
Rithvik Nishad

Reputation: 565

Dart's private members being exposed

I am not sure if something is wrong, or if I've missed out something VERY FUNDAMENTAL. But I AM able to access private constructors, methods, and members of a class outside the class.

class A {
  static final _a = 1;
}

void main() {
  print(A._a);
}

Output:

1

Dart version:

Dart VM version: 2.8.4 (stable) (Unknown timestamp) on "linux_x64"

Screenshot of the test:
Screenshot of the test

Upvotes: 0

Views: 80

Answers (1)

ChessMax
ChessMax

Reputation: 2265

From the docs:

identifiers that start with an underscore (_) are visible only inside the library. Every Dart app is a library, even if it doesn’t use a library directive.

Private means that it's available in the file it's written in and not accessible to other files. So it's not a true private. You can read more about it here.

Upvotes: 2

Related Questions