Deferred loading (also called lazy loading) allows an application to load a library on demand, if and when it’s needed. To lazily load a library, you must first import it using deferred as.
import 'package:deferred/hello.dart' deferred as hello;
When you need the library, invoke loadLibrary() using the library’s identifier.
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 more examples here asynchrony support or visit the asynchrony support part of the language tour.