exact meaning of “library” keyword in dart

update 2018-03-05

For a while now, part of accepts a URI, which reduces the need of library to a few edge cases.

update 2015-11-27

With a recent change, two imported nameless libraries don’t produce a warning anymore. The plan is to make the library declaration entirely optional.


The library declaration is optional. If it is omitted the library name defaults to "".
There are some situations (pub build) where you get an error if two libraries have the same name, so it’s usually good practice to set proper library names.

In a simple command line app consisting of one library it’s usually fine to omit the library declaration.

From the Dart language spec

An implicitly named library has the empty string as its name.

The name of a library is used to tie it to separately compiled parts
of the library (called parts) and can be used for printing and, more
generally, reflection. The name may be relevant for further language
evolution.

Libraries intended for widespread use should avoid name collisions.
Dart’s pub package management system provides a mechanism for doing
so. Each pub package is guaranteed a unique name, effectively
enforcing a global namespace.

Leave a Comment