Dart 3.11 is live!Learn more
Libraries & imports
Guidance on importing and implementing libraries.
Theimport andlibrary directives can help you create a modular and shareable code base. 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 file (plus its parts) is alibraryLibraryA single compilation unit in Dart, made up of aprimary Dart file and its parts.Learn more, even if it doesn't use alibrary directive.
Libraries can be distributed usingpackages.
Dart uses underscores instead of access modifier keywords likepublic,protected, orprivate. While access modifier keywords from other languages provide more fine-grained control, Dart's use of underscores and library-based privacy provides a straightforward configuration mechanism, helps enable an efficient implementation ofdynamic access, and improves tree shaking (dead code elimination).
Using libraries
# Useimport to specify how a namespace from one library is used in the scope of another library.
For example, Dart web apps generally use thedart:js_interop library, which they can import like this:
import'dart:js_interop'; The only required argument toimport is a URI specifying the library. For built-in libraries, the URI has the specialdart: scheme. For other libraries, you can use a file system path or thepackage: scheme. Thepackage: scheme specifies libraries provided by a package manager such as the pub tool. For example:
import'package:test/test.dart';URI stands for uniform resource identifier.URLs (uniform resource locators) are a common kind of URI.
Specifying a library prefix
#If you import two libraries that have conflicting identifiers, then you can specify a prefix for one or both libraries. For example, if library1 and library2 both have an Element class, then you might have code like this:
import'package:lib1/lib1.dart';import'package:lib2/lib2.dart'aslib2;// Uses Element from lib1.Elementelement1=Element();// Uses Element from lib2.lib2.Elementelement2=lib2.Element(); Import prefixes with thewildcard name_ are non-binding, but will provide access to the non-private extensions in that library.
Importing only part of a library
#If you want to use only part of a library, you can selectively import the library. For example:
// Import only foo.import'package:lib1/lib1.dart'showfoo;// Import all names EXCEPT foo.import'package:lib2/lib2.dart'hidefoo;Lazily loading a library
#Deferred loading (also calledlazy loading) allows a web app to load a library on demand, if and when the library is needed. Use deferred loading when you want to meet one or more of the following needs.
- Reduce a web app's initial startup time.
- Perform A/B testing—trying out alternative implementations of an algorithm, for example.
- Load rarely used functionality, such as optional screens and dialogs.
That doesn't mean Dart loads all the deferred components at start time. The web app can download deferred components via the web when needed.
Thedart tool doesn't support deferred loading for targets other than web. If you're building a Flutter app, consult its implementation of deferred loading in the Flutter guide ondeferred components.
To lazily load a library, first import it usingdeferred as.
import'package:greetings/hello.dart'deferredashello; When you need the library, invokeloadLibrary() using the library's identifier.
Future<void>greet()async{awaithello.loadLibrary();hello.printGreeting();} In the preceding code, theawait keyword pauses execution until the library is loaded. For more information aboutasync andawait, check outasynchronous programming.
You can invokeloadLibrary() multiple times on a library without problems. The library is loaded only once.
Keep in mind the following when you use deferred loading:
- A deferred library's constants aren't constants in the importing file. Remember, these constants don't exist until the deferred library is loaded.
- You can't use types from a deferred library in the importing file. Instead, consider moving interface types to a library imported by both the deferred library and the importing file.
- Dart implicitly inserts
loadLibrary()into the namespace that you define usingdeferred asnamespace. TheloadLibrary()function returns aFuture.
Thelibrary directive
# To specify library-leveldoc comments ormetadata annotations, attach them to alibrary declaration at the start of the file.
/// A really great test library.@TestOn('browser')library;Implementing libraries
#SeeCreate Packages for advice on how to implement a package, including:
- How to organize library source code.
- How to use the
exportdirective. - When to use the
partdirective. - How to use conditional imports and exports to implement a library that supports multiple platforms.
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Page last updated on 2025-09-15.View source orreport an issue.