text
stringlengths 1
372
|
|---|
);
|
}
|
}
|
<code_end>
|
import the new dart library
|
with the deferred keyword in your app and
|
call loadLibrary() (see lazily loading a library).
|
the following example uses FutureBuilder
|
to wait for the loadLibrary future (created in
|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.