Sub topic 6
Pre-warming and caching the Flutter engine
Assume you know basic Dart and Flutter, and that you can create an Android Activity or an iOS view controller. This chapter focuses on startup time. It does not cover the full module setup.
Why pre-warm an engine?
A FlutterEngine is the runtime that executes Dart code, loads Flutter widgets, and communicates with the platform. A Flutter screen also needs a Flutter view, which displays the engine's output.
If the native app creates the engine only when the user opens a Flutter screen, startup includes several tasks:
- Starting the Dart runtime.
- Loading the Flutter framework and application code.
- Registering plugins.
- Building the first widget tree.
- Connecting the engine to a view.
This work can make the first Flutter screen feel slow. Pre-warming means starting the engine before the user requests the screen. Caching means keeping the started engine in memory so a later screen can reuse it.
The engine cache is process-local memory. It is lost when the native process is killed. Do not use it as permanent storage for user data.
The engine and view have separate lifecycles
A cached engine can exist without a visible Flutter view. When the user opens a Flutter screen, the host attaches a FlutterActivity or FlutterViewController to that engine.
This separation gives you control over both lifecycles. You can create one engine for the whole application, or create engines for specific features. You should also decide when to destroy an engine. Keeping many engines alive increases memory use.
Android: cache an engine early
An Android application class is a common place to pre-warm an engine. The application class is created before most Activities, so it can start the engine ahead of time.
import android.app.Application
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val engine = FlutterEngine(this)
engine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
FlutterEngineCache.getInstance().put("main_engine", engine)
}
}Declare MyApplication in AndroidManifest.xml:
<application
android:name=".MyApplication"
android:label="Host App">
</application>An Activity can then reuse the engine:
import io.flutter.embedding.android.FlutterActivity
val intent = FlutterActivity
.withCachedEngine("main_engine")
.build(this)
startActivity(intent)The cache key must match exactly. If the key is missing, the Activity cannot reuse the engine. In production code, handle that case instead of using a forced null assertion.
Register method-channel handlers and other engine-specific setup before presenting the Flutter screen. A handler belongs to a particular engine, so registering it on a different engine has no effect.
iOS: retain the engine
On iOS, keep a FlutterEngine in an object whose lifetime matches the native application or feature. Run the engine during initialization, then pass it to a FlutterViewController.
import Flutter
import FlutterPluginRegistrant
import UIKit
final class FlutterDependencies {
let engine = FlutterEngine(name: "main_engine")
init() {
engine.run()
GeneratedPluginRegistrant.register(with: engine)
}
}
final class HomeViewController: UIViewController {
var dependencies: FlutterDependencies!
@objc func showFlutter() {
let flutterViewController = FlutterViewController(
engine: dependencies.engine,
nibName: nil,
bundle: nil
)
present(flutterViewController, animated: true)
}
}The FlutterDependencies instance must remain retained. If it is released, the engine can also be released, and a later attempt to show the screen will fail or create unexpected behavior.
Choosing a caching strategy
| Strategy | Startup | Memory use | Suitable when |
|---|---|---|---|
| Create on demand | Slower first screen | Lower before use | Flutter is rarely opened |
| One pre-warmed engine | Faster first screen | Moderate | Several screens share one Flutter app |
| Several cached engines | Fast feature startup | Highest | Features need isolated state |
A single reused engine keeps navigation and Dart state alive. That can be useful, but stale state may surprise users when they return to a screen. Create a new engine when isolation matters more than startup speed.
Exercise
Why can a cached engine improve startup time, and what is the main cost?
Show answer
It starts the Dart runtime and loads Flutter before the screen is requested, so less work remains at navigation time. The cost is additional memory while the engine remains alive.