For example, Dart web apps generally use the library, which they can import like this:
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:
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
Importing only part of a library
If you want to use only part of a library, you can selectively importthe library. For example:
// Import only foo.
// Import all names EXCEPT foo.
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 lazily load a library, you must firstimport it using deferred as
.
When you need the library, invokeloadLibrary()
using the library’s identifier.
Future greet() async {
await hello.loadLibrary();
hello.printGreeting();
In the preceding code,the await
keyword pauses execution until the library is loaded.For more information about async
and await
,see .
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.
- Dart implicitly inserts
loadLibrary()
into the namespace that you defineusingdeferred as namespace
.TheloadLibrary()
function returns a Future.