Sub topic 12
Several Flutter Screens with FlutterEngineGroup
Assume you know basic Dart and Flutter widgets. You should also know how your native platform creates screens and manages dependencies. This chapter focuses on running multiple independent Flutter screens in one native app.
Why use FlutterEngineGroup?
A FlutterEngine runs Dart code and owns a Flutter isolate. A Flutter view displays the UI produced by that engine. If every Flutter screen creates a completely separate engine, startup work and memory use can increase.
FlutterEngineGroup creates several engines that share some underlying resources. Each engine still has its own Dart isolate, navigation stack, and application state.
| Choice | State | Startup and memory |
|---|---|---|
| One shared engine | Shared Dart state and navigation | Lowest resource use, but screens are coupled |
| Separate engines | Independent state and navigation | More resources |
| Engines from `FlutterEngineGroup` | Independent state with shared engine resources | Useful middle ground |
Use an engine group when users can open separate Flutter surfaces, such as a checkout screen and a profile screen, without one screen inheriting the other's navigation state.
The engine-group model
The native app owns one long-lived engine group. It asks the group for an engine whenever it needs a new Flutter surface. The native app then attaches that engine to a Flutter activity or view controller.
Create one engine for each independently navigable Flutter surface. Do not create a new group for every screen. Keep the group alive for at least as long as the engines it creates.
Creating engines on Android
The following class creates two engines from the same group. Each engine starts the default Dart entrypoint and receives a different initial route.
import android.content.Context
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.FlutterEngineGroup
class FlutterScreenFactory(context: Context) {
private val group = FlutterEngineGroup(context)
fun prepareCart() {
val engine = group.createAndRunEngine(
context,
DartExecutor.DartEntrypoint.createDefault(),
"/cart",
emptyList()
)
FlutterEngineCache.getInstance().put("cart_engine", engine)
}
fun prepareProfile() {
val engine = group.createAndRunEngine(
context,
DartExecutor.DartEntrypoint.createDefault(),
"/profile",
emptyList()
)
FlutterEngineCache.getInstance().put("profile_engine", engine)
}
fun cartIntent(context: Context) =
FlutterActivity.withCachedEngine("cart_engine").build(context)
fun profileIntent(context: Context) =
FlutterActivity.withCachedEngine("profile_engine").build(context)
}The host can call prepareCart() and prepareProfile() during app startup or shortly before navigation. It then starts the matching cached engine. The Flutter module must define both routes in its MaterialApp.
For screens that need different startup logic, create the engine with a named Dart entrypoint instead of createDefault(). Mark that Dart function with @pragma('vm:entry-point') so release compilation keeps it.
Creating engines on iOS
On iOS, FlutterEngineGroup provides the same model. Keep the group as a property, then create and configure each engine before presenting its FlutterViewController.
import Flutter
import FlutterPluginRegistrant
final class FlutterScreenFactory {
private let group = FlutterEngineGroup(name: "screen_engines", project: nil)
func makeCartEngine() -> FlutterEngine {
let engine = group.makeEngine(
withEntrypoint: "main",
libraryURI: nil,
initialRoute: "/cart",
entrypointArgs: nil
)
GeneratedPluginRegistrant.register(with: engine)
return engine
}
func makeProfileEngine() -> FlutterEngine {
let engine = group.makeEngine(
withEntrypoint: "main",
libraryURI: nil,
initialRoute: "/profile",
entrypointArgs: nil
)
GeneratedPluginRegistrant.register(with: engine)
return engine
}
}Attach the returned engine to a FlutterViewController. Register plugins for every engine that uses plugins. A plugin with process-wide native state may still need its own lifecycle design.
State and lifecycle decisions
Treat each engine as a small Flutter application.
- Dispose an engine when its screen and future reuse are no longer needed.
- Do not assume a route change in one engine changes another engine.
- Pass identity or configuration through a platform channel or initial arguments.
- Decide whether engines should be pre-warmed. Pre-warming lowers navigation latency but increases background memory use.
- Test plugin behavior with multiple engines. Some plugins assume that only one Flutter engine exists.
Exercise
A native app has checkout and profile screens that must keep separate navigation stacks. Should it use one shared engine or two engines from one FlutterEngineGroup, and why?
Show answer
It should use two engines from one FlutterEngineGroup. Each engine has an independent Dart isolate and navigation state, while the group allows the engines to share underlying Flutter resources.