Sub topic 8
Deferred loading with `deferred as`
This chapter assumes that you know Dart imports, async and await, and how to build a Flutter app. Deferred loading is useful when an app has features that most users do not open during startup, such as reports, administration, help, or advanced editing.
What deferred loading changes
A normal import makes a library part of the app's initial code load:
import 'report_screen.dart';A deferred import gives the library a namespace and allows it to load later:
import 'report_screen.dart' deferred as report;The code in report_screen.dart can then be loaded by awaiting report.loadLibrary(). Until that future completes, do not create objects or call functions from the deferred library.
Consider this small example.
lib/main.dart:
import 'package:flutter/material.dart';
import 'report_screen.dart' deferred as report;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () async {
await report.loadLibrary();
if (!context.mounted) {
return;
}
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => report.ReportScreen(),
),
);
},
child: const Text('Open report'),
),
),
),
);
}
}lib/report_screen.dart:
import 'package:flutter/material.dart';
class ReportScreen extends StatelessWidget {
const ReportScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Report loaded')),
);
}
}The report. prefix is required because the import is deferred and has an import prefix. The library is loaded only when the button is pressed. A later call to loadLibrary() reuses the loaded library.
Where deferred loading works
The result depends on the target platform and its packaging support. On the web, deferred Dart libraries can become separate JavaScript loading units. On Android, deferred components can be delivered through supported Android App Bundle configurations. Other targets may package the code together, so the syntax remains valid but provides little or no download benefit.
| Question | Effect |
|---|---|
| Is the feature rarely used? | Deferred loading may shorten the initial path. |
| Does the target support loading units? | The feature can be delivered or loaded separately. |
| Does the deferred library share many dependencies? | The size reduction may be small. |
| Can the feature be unavailable offline? | Add an error state and retry option. |
Common errors and trade-offs
A deferred library is not a substitute for removing unused code. Its dependencies may still be placed in the initial loading unit if other parts of the app use them. Large packages that are shared everywhere do not move cleanly into a deferred feature.
The first opening of a deferred feature can take longer. Show progress while loadLibrary() runs, and catch failures caused by missing network access or a failed download.
Use deferred loading when the feature is substantial, optional, and infrequently used. Measure the result with the target's size analysis tools. A split that adds complexity but removes only a small amount of startup code is usually not worth keeping.
Exercise
Why must code await report.loadLibrary() before constructing report.ReportScreen?
Show answer
The deferred library has not been loaded yet. Awaiting loadLibrary ensures that its classes and functions are available before they are used.