Using libraries

Use import to specify how a namespace from one library is used in thescope of another library.

For example, Dart web apps generally use the dart:htmllibrary, which they can import like this:

  1. import 'dart:html';

The only required argument to import is a URI specifying thelibrary.For built-in libraries, the URI has the special dart: scheme.For other libraries, you can use a file system path or the package:scheme. The package: scheme specifies libraries provided by a packagemanager such as the pub tool. For example:

  1. import 'package:test/test.dart';

Note: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 youcan specify a prefix for one or both libraries. For example, if library1and library2 both have an Element class, then you might have code likethis:

  1. import 'package:lib1/lib1.dart';
  2. import 'package:lib2/lib2.dart' as lib2;
  3. // Uses Element from lib1.
  4. Element element1 = Element();
  5. // Uses Element from lib2.
  6. lib2.Element element2 = lib2.Element();

Importing only part of a library

If you want to use only part of a library, you can selectively importthe library. For example:

  1. // Import only foo.
  2. import 'package:lib1/lib1.dart' show foo;
  3. // Import all names EXCEPT foo.
  4. import 'package:lib2/lib2.dart' hide foo;

Lazily loading a library

Deferred loading (also called lazy loading)allows a web app to load a library on demand,if and when the library is needed.Here are some cases when you might use deferred loading:

  • To reduce a web app’s initial startup time.
  • To perform A/B testing—trying outalternative implementations of an algorithm, for example.
  • To load rarely used functionality, such as optional screens and dialogs.

Only dart2js supports deferred loading. Flutter, the Dart VM, and dartdevc don’t support deferred loading. For more information, see issue #33118 and issue #27776.

To lazily load a library, you must firstimport it using deferred as.

  1. import 'package:greetings/hello.dart' deferred as hello;

When you need the library, invokeloadLibrary() using the library’s identifier.

  1. Future greet() async {
  2. await hello.loadLibrary();
  3. hello.printGreeting();
  4. }

In the preceding code,the await keyword pauses execution until the library is loaded.For more information about async and await,see asynchrony support.

You can invoke loadLibrary() 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 byboth the deferred library and the importing file.
  • Dart implicitly inserts loadLibrary() into the namespace that you defineusing deferred as namespace.The loadLibrary() function returns a Future.