TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
library directive is a file-level declaration used to explicitly identify a Dart library and provide a syntactic anchor for library-scoped metadata and documentation. While the Dart compiler implicitly treats every .dart file as a distinct library, the explicit library directive formalizes this boundary and acts as an Abstract Syntax Tree (AST) node for file-level annotations.
Lexical Placement
If present, thelibrary directive must be the first declaration in a Dart file. It is a structural directive, not executable code, and must appear strictly before any import, export, or part directives. The only elements permitted to precede the library directive are an optional script tag (#!), comments (including /// documentation comments), and its own metadata annotations.
Syntax and Naming
Since Dart 2.19, thelibrary directive can be unnamed. This is the recommended syntax when the directive is only needed to attach metadata or documentation, as it avoids inventing a redundant identifier.
library keyword, it must adhere to specific rules:
- Standard: The Dart style guide mandates
snake_case(lowercase letters separated by underscores) for library names. - Dot Notation: Syntactically, Dart allows dot-separated identifiers (e.g.,
library my_package.core.network;). While considered legacy syntax, it is explicitly permitted by the standardlibrary_nameslint rule. However, if the modernunnecessary_library_namelint is enabled, the analyzer will flag all named libraries (includingsnake_caseones) in favor of the unnamedlibrary;syntax.
Implicit vs. Explicit Libraries
Thelibrary directive is entirely optional. If omitted, the Dart analyzer and compiler automatically generate an implicit library based on the package name and the file’s URI path. The explicit directive is mechanically required only to provide a target for library-level metadata (@) or Dartdoc documentation (///).
Interaction with Part Directives
Historically, a namedlibrary directive was required to establish a bidirectional relationship between a parent file and its constituent parts using part and part of.
part of 'parent_module.dart';, which bypasses the need for a named library identifier entirely.
Master Dart with Deep Grasping Methodology!Learn More





