Question

In Dart-land, what is the relationship between a library declaration and the lib directory inside the project? In other words, if my project has a structure like this:

myapp/
    lib/
        SomeType.dart
        src/
            SomeOtherType.dart
    web/
        AnotherType.dart
        another-type.html

And in SomeType.dart I have:

library myapp.logging;

class SomeType {
    // ...
}

...then what's the difference between the myapp/lib directory and any library declarations sprinkled throughout the Dart source code?

Was it helpful?

Solution

Ultimately, there is no relation between your directory structure and the library keyword in Dart.

The dart directory structure comes from the recommended package layout conventions but are not enforced by the language in any way. They are only enforced if you try to use the pub package manager to publish (upload) your package to the official dart repository. Ultimately you can use whatever directory structure layout you choose. This one is recommended to keep consistency between packages, particularly for Open Source packages or applications which may have other external contributors.

The library keyword in Dart is used in a way of namespacing. For instance the underscore private variables are only have library visibility. Anything in the same library can see/access your underscore variables. Anything outside of it cannot. You can use the part and part of keywords to make other files part of a library (regardless of where they are located in the directory structure).

OTHER TIPS

The lib directory has no relationship with the library declaration. You can declare a library consisting of one or more dart files no matter in which directory the files are stored. Several files consisting one library don't need even to be stored in the same directory.

If you want to make a package available as a library the convention is to store the files consisting the public API in the lib directory and library private API in lib/src. Even here there could be several libraries in lib and several libraries in lib/src.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top