Skip to main content
A Dart library prefix is an explicit lexical namespace identifier assigned to an imported library using the as keyword. It encapsulates the imported library’s top-level members (classes, functions, variables, and typedefs) within a designated scope, requiring those members to be accessed via dot notation.

Syntax

Once prefixed, members of the imported library are accessed using the prefix_identifier.member syntax:

Technical Mechanics

  • Namespace Binding: By default, Dart imports declarations directly into the library namespace (or library scope) of the importing file. Applying a prefix alters this behavior, binding the library’s export scope strictly to the defined identifier. A prefix is strictly a lexical namespace, not a first-class object. It cannot be assigned to variables, passed as an argument, or manipulated like an object.
  • Identifier Resolution: When the Dart compiler encounters prefix.identifier, it restricts the lexical lookup for identifier exclusively to the scope bound to prefix.
  • Shared Namespaces: Dart allows multiple libraries to be imported under the same prefix. Doing so merges the exported members of all those libraries into a single, shared lexical namespace.
  • Extension Resolution: When a library containing Dart Extensions is imported with a prefix, its extension methods are hidden from implicit resolution and are no longer automatically applied to types. They must be invoked explicitly using the prefix and the extension’s name.
  • Combinator Interaction: Prefixes can be used in conjunction with show and hide combinators. The Dart analyzer applies the combinator filters to the library’s API before binding the remaining members to the prefix namespace.
  • Deferred Loading Requirement: A library prefix is syntactically mandatory when implementing deferred (lazy) loading via the deferred as keywords. The prefix provides the lexical namespace required to invoke the asynchronous loadLibrary() method, which the Dart runtime uses to load the library into memory.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More