text
stringlengths
1
474
download additional Dart code and assets at runtime.
This allows apps to reduce install apk size and download
features and assets when needed by the user.We refer to each uniquely downloadable bundle of Dart
libraries and assets as a “deferred component”.
To load these components, use Dart’s deferred imports.
They can be compiled into split AOT and JavaScript shared libraries.info Note
Flutter supports deferred, or “lazy”, loading on Android and the web.
The implementations differ.
Android’s dynamic feature modules deliver the
deferred components packaged as Android modules.
The web creates these components as separate *.js files.
Deferred code doesn’t impact other platforms,
which continue to build as normal with all deferred
components and assets included at initial install time.Though you can defer loading modules,
you must build the entire app and upload that app as a single
Android App Bundle (*.aab).
Flutter doesn’t support dispatching partial updates without re-uploading
new Android App Bundles for the entire application.Flutter performs deferred loading when you compile your app
in release or profile mode.
Debug mode treats all deferred components as regular imports.
The components are present at launch and load immediately.
This allows debug builds to hot reload.For a deeper dive into the technical details of
how this feature works, see Deferred Components
on the Flutter wiki.<topic_end>
<topic_start>
How to set your project up for deferred components
The following instructions explain how to set up your
Android app for deferred loading.<topic_end>
<topic_start>
Step 1: Dependencies and initial project setup
Add Play Core to the Android app’s
build.gradle dependencies.
In android/app/build.gradle add the following:If using the Google Play Store as the
distribution model for dynamic features,
the app must support SplitCompat and provide an instance
of a PlayStoreDeferredComponentManager.
Both of these tasks can be accomplished by setting
the android:name property on the application in
android/app/src/main/AndroidManifest.xml to
io.flutter.embedding.android.FlutterPlayStoreSplitApplication:io.flutter.app.FlutterPlayStoreSplitApplication handles
both of these tasks for you. If you use
FlutterPlayStoreSplitApplication,
you can skip to step 1.3.If your Android application
is large or complex, you might want to separately support
SplitCompat and provide the
PlayStoreDynamicFeatureManager manually.To support SplitCompat, there are three methods
(as detailed in the Android docs), any of which are valid:Make your application class extend
SplitCompatApplication:Call SplitCompat.install(this);
in the attachBaseContext() method:Declare SplitCompatApplication as the application
subclass and add the Flutter compatibility code from
FlutterApplication to your application class:The embedder relies on an injected
DeferredComponentManager instance to handle
install requests for deferred components.
Provide a PlayStoreDeferredComponentManager into
the Flutter embedder by adding the following code
to your app initialization:Opt into deferred components by adding
the deferred-components entry to the app’s pubspec.yaml
under the flutter entry:The flutter tool looks for the deferred-components
entry in the pubspec.yaml to determine whether the
app should be built as deferred or not.
This can be left empty for now unless you already
know the components desired and the Dart deferred libraries
that go into each. You will fill in this section later
in step 3.3 once gen_snapshot produces the loading units.<topic_end>
<topic_start>
Step 2: Implementing deferred Dart libraries
Next, implement deferred loaded Dart libraries in your
app’s Dart code. The implementation does not need
to be feature complete yet. The example in the
rest of this page adds a new simple deferred widget
as a placeholder. You can also convert existing code
to be deferred by modifying the imports and
guarding usages of deferred code behind loadLibrary()
Futures.Create a new Dart library.
For example, create a new DeferredBox widget that
can be downloaded at runtime.
This widget can be of any complexity but,
for the purposes of this guide,
create a simple box as a stand-in.
To create a simple blue box widget,
create box.dart with the following contents:
<code_start>// box.dart
import 'package:flutter/material.dart';
/// A simple blue 30x30 box.
class DeferredBox extends StatelessWidget {
const DeferredBox({super.key});
@override
Widget build(BuildContext context) {
return Container(
height: 30,
width: 30,
color: Colors.blue,
);
}
}<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