Home > Software design >  Can I use library keyword in personal project but not in library
Can I use library keyword in personal project but not in library

Time:01-14

I am using Flutter. As you are aware, Flutter doesn't have protected keyword to make classes package private. But interestingly, the same could be achieved using library, part, part of keywords once I name widgets starting with underscore. But, I am a bit worried about performance issues.

Question: Is it ok to use library/part/part of keywords inside ordinary projects?

CodePudding user response:

public classes inside lib/src are considered package private. Only classes in lib are truly public, so are files in lib/src when they get exported by a file in lib.

enchilada/
  lib/
    enchilada.dart <-- public
    src/
      beans.dart <-- package private unless exported

While technically you can access everything in lib/src, you get a warning when you use Warning: Don't import implementation files from another package.

The part keyword can not be used to hide code, it inherits the same visibility as the library. All it does is splitting a file into multiple files. It should only be used for code generation these days

Visibility doesn't affect performance in any way.

CodePudding user response:

Naming your entity starting with an underscore (for example _test) should make it private.

Libraries not only provide APIs, but are a unit of privacy: 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.

Source

  •  Tags:  
  • Related