Reporting a bug in dartpad

18 views
Skip to first unread message

Kashif Umar

unread,
May 30, 2023, 5:48:24 AM5/30/23
to Dart Misc

Hello Dart team,

I hope you are doing well. I recently encountered an interesting situation while experimenting with encapsulation in Dart, which is one of the fundamental concepts of object-oriented programming. Surprisingly, when I attempted to access private fields and methods, the program did not throw an error. This behavior was consistent not only with private variables but also with private methods.

I conducted this experiment using Flutter in Visual Studio Code, and the program executed and throw errors in accessing private members of the class. However, when I tried the same code in DartPad, the private fields, and methods were accessible.

I have attached a screenshot from DartPad demonstrating my successful access and manipulation of private variables and methods.

Thank you for your attention.

Best regards,

Muhammad Kashif Umar

Screenshot 2023-05-27 at 6.48.09 PM.png

Brett Morgan

unread,
May 30, 2023, 6:03:39 AM5/30/23
to mi...@dartlang.org
Private in Dart is different to private in Java and similar languages. In Java, private means access is restricted to the class. In Dart, privacy is constrained to accessing inside the library. In your case, both the class and the test code are in the same file, and thus in the same library. 

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/70ab2253-f83c-4af4-ad8a-915d9918f85an%40dartlang.org.

Victor Nwakutere

unread,
May 30, 2023, 6:15:02 AM5/30/23
to mi...@dartlang.org
Hi,

You can allow access private properties and methods a class inside the class itself in dart.

Do well to review library in dart to understand more in dart.

--
Reply all
Reply to author
Forward
0 new messages