…
}
- 在 `attachBaseContext()` 中调用 `SplitCompat.install(this);`:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// Emulates installation of future on demand modules using SplitCompat.
SplitCompat.install(this);
}
- 将 `SplitCompatApplication` 声明为 application 的子类, 并将 `FlutterApplication` 中的 flutter 兼容性代码添加到你的 application 类中:
<application
…
android:name=“com.google.android.play.core.splitcompat.SplitCompatApplication”>
嵌入层依赖注入的 DeferredComponentManager
实例来处理延迟组件的安装请求。 通过在应用程序的初始流程中添加以下代码,将 PlayStoreDeferredComponentManager
添加到 Flutter 嵌入层中:
import io.flutter.embedding.engine.dynamicfeatures.PlayStoreDeferredComponentManager;
import io.flutter.FlutterInjector;
…
layStoreDeferredComponentManager deferredComponentManager = new
PlayStoreDeferredComponentManager(this, null);
FlutterInjector.setInstance(new FlutterInjector.Builder()
.setDeferredComponentManager(deferredComponentManager).build());
- 通过将 `deferred-components` 依赖添加到应用程序的 `pubspec.yaml` 中的 `flutter` 下,并选择延迟组件:
…
flutter:
…
deferred-components:
…
flutter
工具会在 pubspec.yaml
中查找 deferred-components
, 来确定是否应将应用程序构建为延迟加载。 除非你已经知道所需的组件和每个组件中的 Dart 延迟库,否则可以暂时将其留空。 当 gen_snapshot
生成加载单元后,你可以在后面的 步骤 3.3
中完善这部分内容。
步骤 2:实现延迟加载的 Dart 库
接下来,在 Dart 代码中实现延迟加载的 Dart 库。实现并非立刻需要的功能。 文章剩余部分中的示例添加了一个简单的延迟 widget 作为占位。 你还可以通过修改 loadLibrary()
和 Futures
后面的延迟加载代码的导入和保护用法,将现有代码转换为延迟代码。
- 创建新的 Dart 库。例如,创建一个可以在运行时下载的 `DeferredBox` widget。 这个 widget 可以是任意复杂的,本指南使用以下内容创建了一个简单的框。
// box.dart
import ‘package:flutter/widg