Sub topic 13
Limitations and trade-offs to plan for
Earlier chapters showed how to build a module, host it in Android or iOS, cache the engine, and exchange data over channels. This chapter covers what that setup costs and where it stops working. It assumes you have read those chapters. You should know what a FlutterEngine is, what a cached engine does, and how a module is packaged. The goal is to help you decide how much Flutter to put in a native app, and to spot the problems before they reach production.
Where the costs come from
Most add-to-app limitations come from one fact. A native app that hosts Flutter ships a second runtime next to its own. That runtime has its own engine, its own Dart heap, its own rendering pipeline, and its own build toolchain. Each limitation below is a consequence of that.
Memory
A FlutterEngine holds a Dart VM instance, a rendering surface, and the Dart heap for your code. Pre-warming an engine, as covered in the caching chapter, trades memory for startup speed. The engine stays alive whether or not the user ever opens a Flutter screen.
Three rules help here:
- Pre-warm only the engine you expect the user to need soon. Do not pre-warm one per screen.
- Release a cached engine when its flow is finished. A cached engine is never destroyed for you, even after the activity that used it is gone.
- Treat memory pressure as a real event. On Android, react to
onTrimMemory. On iOS, react to memory warnings.
This Kotlin helper removes a cached engine and frees its resources:
import io.flutter.embedding.engine.FlutterEngineCache
fun releaseFlutterEngine(id: String = "main_engine") {
val cache = FlutterEngineCache.getInstance()
cache.get(id)?.destroy()
cache.remove(id)
}Call it when the user leaves the Flutter part of the app for good. Do not call it between two screens of the same flow, because you would pay the startup cost again.
Several independent engines multiply the memory cost. FlutterEngineGroup exists to reduce it. Engines created from one group share parts of their runtime. The Flutter documentation describes the memory added by each extra engine in a group as small compared with a standalone engine. Measure this on your own app, because the figure depends on the Flutter version and on what each screen loads.
App size
Flutter adds its engine to your binary. On Android, the release build includes native libraries for each ABI you ship. On iOS, it adds Flutter.framework and your compiled Dart code.
Ways to keep the increase in check:
- On Android, publish an Android App Bundle so each device downloads only its own ABI.
- Restrict
abiFiltersto the architectures you actually support. - Remove unused plugins and assets from the module. Each plugin adds native code to the host.
- Compare the size of the release build before and after adding Flutter. Do not estimate from the debug build.
If the Flutter part is one small screen, the size cost may be larger than the screen is worth. That is a legitimate reason to build it natively.
State and isolation
Each engine runs its own isolate. An isolate is Dart's unit of execution with its own memory, and isolates cannot share objects directly. In add-to-app, this has practical consequences:
- Static variables, singletons, and providers in Dart are separate in each engine. Two screens on two engines do not see the same
Providerorstaticfield. - Dart cannot share objects with native code. Data crosses the boundary only as serialized messages over a channel.
- Even within one engine, a native screen that is destroyed does not clear Dart state. A cached engine keeps its Dart state, including the navigator stack, between visits.
The last point causes common bugs. A user opens the Flutter cart, backs out, and reopens it from a different place. The cached engine still holds the previous route stack, so the screen can appear at the wrong place. Set the route explicitly each time you present the screen, instead of relying on the entrypoint's initial route. Alternatively, keep a clear rule about when the engine resets.
When several native screens need the same data, choose a single source of truth and keep it on the native side or behind a shared service. Then push changes to each engine through a channel. Do not try to keep several Dart copies in sync.
Platform behavior and plugins
The Flutter tooling assumes it owns the whole app, and some plugins make the same assumption. Check each plugin you need against the host setup.
| Area | What can go wrong | What to do |
|---|---|---|
| Plugins that need an Android `Activity` | A plugin may expect to attach to a `FlutterActivity` and fail when the engine is not attached to an activity | Test each plugin with your real host setup, including a pre-warmed engine with no activity yet |
| Plugin registration on iOS | A plugin is unavailable because `GeneratedPluginRegistrant.register(with:)` was not called on the engine | Register plugins on every engine you create |
| Back navigation | Android's back button may close the native screen instead of popping a Flutter route | Use `FlutterActivity`'s back handling or forward back events to Flutter explicitly |
| System UI and insets | The status bar, navigation bar, and safe areas may differ from the native screens around them | Check `MediaQuery` padding on devices with notches and gesture navigation |
| Text scale and accessibility | Native and Flutter screens may react differently to font size and screen reader settings | Test both screens with large text and TalkBack or VoiceOver |
| Platform views | Embedding native views inside Flutter inside a native app has performance and layering costs | Avoid nesting where you can |
Also treat lifecycle events as a source of bugs. When the host app goes to the background, the engine is told through the lifecycle channel. If the process is killed and restored, the native side restores its screens but the Dart state is gone. Decide which state must survive process death, and store it in native or persistent storage instead of Dart memory.
Tooling, builds, and team
The module and the host have different build systems. The module is built by the Flutter tool. The Android host is built by Gradle and the iOS host by Xcode. Each integration option shifts cost between those systems.
| Packaging option | Host developers need | Main cost |
|---|---|---|
| Module as source (Gradle include, CocoaPods path) | The Flutter SDK installed | Every native developer needs a working Flutter setup |
| Prebuilt AAR (Android) | Only the published artifacts | A publish step, and version coordination between module and host |
| Prebuilt frameworks (iOS) | Only the frameworks | Same, plus handling of every plugin's framework |
Other points to plan for:
- Pin the Flutter SDK version. The module, the AAR or frameworks, and the CI machines must all use the same one. An engine from one version and Dart code compiled with another will not run.
- Keep your debug workflow documented.
flutter attachneeds the host app running in debug mode and the module reachable. Release builds do not support it. - Expect CI to take longer. A pipeline now builds the module first, then the host, and may need caches for both Gradle and Pub.
- Review who owns the module. If a mobile team splits between native and Flutter developers, plan for the channel API as a contract with versioning, as covered in the Pigeon chapter.
Constraints to check before committing
Some limits come from the tooling and change with Flutter releases. Check the current add-to-app documentation for each of these before you commit:
- Support for more than one Flutter module in one host app. Plan on a single module that contains all your Flutter screens.
- Supported CPU architectures for release builds on Android.
- Minimum supported Android and iOS versions.
- Compatibility with your host's build setup, such as Swift Package Manager or a specific Gradle plugin version.
- Whether your needed plugins support add-to-app use.
Treat these as a short compatibility checklist you run on a prototype. Do not run it on paper.
A decision guide
A practical way to decide:
- Start with the smallest slice. Pick one self-contained screen or flow, not a screen that is tightly coupled to native state.
- Build a prototype with your real host, your real plugins, and a release build. Measure startup time, memory, and size on the slowest device you support.
- Count the interfaces. Each channel method is a contract you must version and test on both platforms.
- Choose an engine strategy. One cached engine is simplest. Use an engine group only when you have several entry points that need to coexist.
- Re-evaluate when the Flutter portion grows. If Flutter ends up covering most of the app, moving the shell to Flutter may cost less than maintaining the boundary.
Add-to-app is a good fit when a team wants to share one UI codebase for a feature across both platforms, or to migrate a native app gradually. It is a poor fit for a single small widget, for performance-critical native screens, or when the team cannot support a second toolchain.
Exercise
A team caches one engine under the ID "main_engine" and uses it for a cart screen and a profile screen. Users report that opening the profile screen sometimes shows the cart screen's last page. Give the cause and two ways to fix it.
Show answer
Both screens share one engine, so they share one Dart navigator stack. The engine keeps its state between visits, so it shows whichever route was active last. Fixes: set the route explicitly each time a screen is presented (for example, with a channel call or setInitialRoute before the engine runs a new flow), or use separate engines created from a FlutterEngineGroup, each with its own entrypoint and initial route. A third option is to destroy and recreate the engine when switching flows, at the cost of startup time.
Exercise
Your release build on Android grew by a large amount after adding a Flutter module. Name two steps to reduce the download size users see.
Show answer
Publish an Android App Bundle so each device downloads only the native libraries for its own ABI, and limit abiFilters to the architectures you support. Removing unused plugins and assets from the module also reduces size.