Khamidjon Khamidov
Khamidjon Khamidov

Reputation: 8989

Can I use library keyword in personal project but not in library

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?

Upvotes: 0

Views: 815

Answers (2)

passsy
passsy

Reputation: 5222

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 implementation files.

Warning: Don't import implementation files from another package.

Don't use part

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 https://stackoverflow.com/a/27764138/669294

Note: You may have heard of the part directive, which allows you to split a library into multiple Dart files. We recommend that you avoid using part and create mini libraries instead.

source

Performance

Visibility doesn't affect performance in any way.

Upvotes: 1

nvoigt
nvoigt

Reputation: 77354

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

Upvotes: 0

Related Questions