text
stringlengths
1
474
initState) to complete and display a
CircularProgressIndicator as a placeholder.
When the Future completes, it returns the DeferredBox widget.
SomeWidget can then be used in the app as normal and
won’t ever attempt to access the deferred Dart code until
it has successfully loaded.
<code_start>import 'package:flutter/material.dart';
import 'box.dart' deferred as box;
class SomeWidget extends StatefulWidget {
const SomeWidget({super.key});
@override
State<SomeWidget> createState() => _SomeWidgetState();
}
class _SomeWidgetState extends State<SomeWidget> {
late Future<void> _libraryFuture;
@override
void initState() {
super.initState();
_libraryFuture = box.loadLibrary();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<void>(
future: _libraryFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
return box.DeferredBox();
}
return const CircularProgressIndicator();
},
);
}
}<code_end>
The loadLibrary() function returns a Future<void>
that completes successfully when the code in the library
is available for use and completes with an error otherwise.
All usage of symbols from the deferred library should be
guarded behind a completed loadLibrary() call. All imports
of the library must be marked as deferred for it to be
compiled appropriately to be used in a deferred component.
If a component has already been loaded, additional calls
to loadLibrary() complete quickly (but not synchronously).
The loadLibrary() function can also be called early to
trigger a pre-load to help mask the loading time.You can find another example of deferred import loading in
Flutter Gallery’s lib/deferred_widget.dart.<topic_end>
<topic_start>
Step 3: Building the app
Use the following flutter command to build a
deferred components app:This command assists you by validating that your project
is properly set up to build deferred components apps.
By default, the build fails if the validator detects
any issues and guides you through suggested changes to fix them.info Note
You can opt out of building deferred components
with the --no-deferred-components flag.
This flag causes all assets defined under
deferred components to be treated as if they were
defined under the assets section of pubspec.yaml.
All Dart code is compiled into a single shared library
and loadLibrary() calls complete in the next event
loop boundary (as soon as possible while being asynchronous).
This flag is also equivalent to omitting the deferred-components:
entry in pubspec.yaml.The
flutter build appbundle command
runs the validator and attempts to build the app with
gen_snapshot instructed to produce split AOT shared libraries
as separate .so files. On the first run, the validator will
likely fail as it detects issues; the tool makes
recommendations for how to set up the project and fix these issues.The validator is split into two sections: prebuild
and post-gen_snapshot validation. This is because any
validation referencing loading units cannot be performed
until gen_snapshot completes and produces a final set
of loading units.info Note
You can opt to have the tool attempt to build your
app without the validator by passing the
--no-validate-deferred-components flag.
This can result in unexpected and confusing
instructions to resolve failures.
This flag is meant to be used in
custom implementations that do not rely on the default
Play-store-based implementation that the validator checks for.The validator detects any new, changed, or removed
loading units generated by gen_snapshot.
The current generated loading units are tracked in your
<projectDirectory>/deferred_components_loading_units.yaml file.
This file should be checked into source control to ensure that
changes to the loading units by other developers can be caught.The validator also checks for the following in the
android directory:<projectDir>/android/app/src/main/res/values/strings.xml
An entry for every deferred component mapping the key
${componentName}Name to ${componentName}.
This string resource is used by the AndroidManifest.xml
of each feature module to define the dist:title property.
For example:<projectDir>/android/<componentName>
An Android dynamic feature module for
each deferred component exists and contains a build.gradle
and src/main/AndroidManifest.xml file.
This only checks for existence and does not validate
the contents of these files. If a file does not exist,
it generates a default recommended one.<projectDir>/android/app/src/main/res/values/AndroidManifest.xml