Sub topic 1
What add-to-app is and when to choose it
Add-to-app is Flutter's supported way to put Flutter screens inside an existing native Android or iOS application. The native app stays the owner of the process, the launch sequence, and most of its navigation. Flutter supplies one or more screens, or parts of screens, that the native code opens when it needs them.
This chapter explains the working model and the decision criteria. Later chapters in this tutorial cover the how-to steps for Android, iOS, engine caching, channels, and release builds.
Prerequisites
This chapter assumes you already have the following:
- Basic Flutter and Dart knowledge: widgets,
StatefulWidget, and running an app with thefluttercommand-line tool. - Working knowledge of either Android (Kotlin or Java, Gradle, Activities) or iOS (Swift or Objective-C, Xcode, view controllers).
- Familiarity with the command line and a dependency manager (Gradle on Android, CocoaPods on iOS).
You do not need to know any add-to-app APIs yet. You will meet them in the next chapters.
The core idea
A normal Flutter app is created with flutter create. Flutter generates the android/ and ios/ folders for you, and the Flutter framework owns the whole app. Add-to-app reverses this ownership. Your native project is the main app. Flutter code is built as a separate project called a module, and the native app consumes that module as a dependency.
Three pieces make this work:
- The Flutter module is a Dart project, created with a special template, that the native build can import. The next chapter covers it.
- A Flutter engine is the runtime that executes Dart code and renders the UI. The native app creates and owns it.
- A Flutter view host is a native component,
FlutterActivityorFlutterFragmenton Android andFlutterViewControlleron iOS. It draws the engine's output on screen and forwards touch and lifecycle events to it.
The native app and the Dart code stay separate programs that share one process. They exchange data only through explicit messages, which are called platform channels. Chapter 7 covers those.
What you can and cannot embed
Add-to-app is flexible about how much of the UI is Flutter.
- A full screen. The most common case. A button in a native screen opens a
FlutterActivityor aFlutterViewControllerthat shows a Flutter route. - A partial screen. On Android, a
FlutterFragmentcan fill part of an Activity. On iOS, aFlutterViewControllercan be added as a child view controller with a restricted frame. - Several Flutter screens. An app can show many Flutter screens at different points in its navigation. Each one can use its own entrypoint and initial route.
Add-to-app does not make Flutter widgets behave like native views inside a native layout tree. A Flutter view is a single rectangular surface that draws its own content. A native RecyclerView row or a UITableViewCell cannot easily contain a Flutter widget, and the cost of many small Flutter views is high. That is covered under limitations later in the tutorial.
How it compares with other approaches
The decision is rarely between "Flutter" and "native". It is usually between several ways of changing a large codebase.
| Approach | What you do | Main benefit | Main cost |
|---|---|---|---|
| Full rewrite in Flutter | Replace the native app with a new Flutter app | One codebase, no host integration | Long delay before users see value, high risk |
| Add-to-app | Keep the native app, add Flutter for selected screens | Incremental adoption, reuse of the existing app | Two toolchains, extra binary size, integration work |
| Stay fully native | Keep building each platform separately | No new technology, smallest binary | Duplicate work on every feature |
| Share logic only (for example Kotlin Multiplatform) | Share business code, keep native UI | Native look and feel everywhere | UI is still written twice |
Add-to-app sits between a rewrite and staying native. It lets a team test Flutter on a real feature before committing to a larger migration.
When add-to-app is a good choice
Choose add-to-app when most of these statements are true:
- You have a large, working native app. Rewriting it would take a long time and give users nothing new during that time.
- A feature is shared across both platforms. Examples are an onboarding flow, a checkout, a settings area, or a content feed. Writing it once in Dart saves effort on both Android and iOS.
- The feature changes often. Screens that product teams update every sprint benefit most from hot reload and a single implementation.
- A separate team can own the module. A Flutter team can ship a module with a clear interface and without touching the native code of other teams.
- The screens are mostly self-contained. They need a small amount of data from the host, such as a user ID or an auth token, and return a small amount of data, such as a result or an event.
- You want to trial Flutter. A single embedded screen is a low-risk way to measure the effect on build time, binary size, and developer speed.
When to think twice
Add-to-app is a weaker fit in these situations:
- The feature depends heavily on native UI. Screens built around maps, camera previews, complex system widgets, or deep platform-specific behavior need constant bridging.
- The app is small. If the native app has a handful of screens, a full Flutter app may be simpler than maintaining two toolchains.
- Startup time is critical on the Flutter screen. The first Flutter screen needs an engine. Without pre-warming, there is a visible delay. Pre-warming costs memory. The engine caching chapter covers this trade-off.
- Binary size is tightly limited. The Flutter engine and framework add several megabytes to your app, and this applies even if only one screen uses them.
- The team has no one who knows Flutter. The module will need maintenance, and the native engineers will have to learn Dart and the build integration.
- Many tiny Flutter views are needed. Each independent view needs its own engine or a shared engine with careful handling, and both have costs.
What the integration changes in your project
Adding Flutter changes the build and the app at runtime, so it helps to know what to expect before you start.
Build changes
- On Android, your Gradle settings include the module as a subproject, or you depend on a prebuilt AAR (Android Archive). Either way, Gradle builds Dart code through Flutter's tooling.
- On iOS, your Xcode project includes the module's frameworks, usually through CocoaPods or by embedding prebuilt frameworks.
- Your CI machines need the Flutter SDK installed at a version that matches the module.
- Flutter plugins used in the module are pulled in as native dependencies. They can conflict with libraries already present in the host app.
Runtime changes
- A Flutter engine uses memory when it runs, even before any Flutter screen is shown. You choose when to start it.
- Dart code runs on its own threads and in its own isolate. The host app's main thread still handles native UI.
- Navigation across the boundary is native navigation. The native app opens a Flutter screen, and the Flutter screen asks the host to close it. Flutter's
Navigatoronly manages routes inside the Flutter view. - State is not shared automatically. If the native app and the module both need the signed-in user, you must pass it across.
A mental model for the boundary
Think of the module as a small web service that runs in your process. It has its own code, its own state, and a defined interface. The native app calls it, sends it data, and receives events. The interface is where most problems appear, so keep it narrow.
A useful rule is to decide, for each feature, who owns each of these:
- The data. Does the module fetch it, or does the host give it to the module?
- The navigation. Which side decides what screen comes next?
- The authentication state. Who holds the token, and who refreshes it?
- The analytics events. Which side logs them, and in what format?
Settle these questions before writing code. Teams that skip them often build a chatty interface with many channel calls, which is hard to test and slow to change.
Common mistakes
- Treating add-to-app as a full Flutter app. The module does not own the app lifecycle, the status bar behavior at launch, or deep links. The host decides these.
- Starting an engine for every screen. Each engine is a heavy resource. Plan to share engines, as the later engine chapters show.
- Duplicating state. Keeping the same user data in Dart and in native code without a single source of truth leads to bugs that depend on timing.
- Ignoring versions. The Flutter version in CI, in each developer's machine, and in the module must match. Mismatches cause build failures that are hard to read.
- Skipping the release build. Debug builds are slower and bigger than release builds. Measure startup and size with release builds only.
Where the rest of the tutorial goes
The next chapters follow the order in which you will meet these ideas. You first create the module, then learn the engine and view model. After that you integrate on Android and iOS, speed up startup with caching, and connect the two sides with channels. Later chapters cover entrypoints and routes, debugging, release packaging, Pigeon, engine groups, and the limitations you should plan for.
Exercise
Your team owns a 200-screen native shopping app. Product wants a new checkout flow that must look and behave the same on Android and iOS, and it will change every two weeks. A colleague proposes rewriting the whole app in Flutter. Give two reasons add-to-app is a better fit, and one risk you would check first.
Show answer
Reasons: (1) The app is large and working, so a full rewrite would take a long time and deliver no user value meanwhile, while add-to-app lets you ship Flutter for just the checkout. (2) The checkout is a self-contained, frequently changing feature shared by both platforms, so a single Dart implementation with hot reload saves duplicate work. Risk to check first: the startup delay of the first Flutter screen and the added binary size, measured in a release build. A related risk is how the checkout gets data such as the cart and auth token from the host, which should be defined as a narrow interface.
Exercise
Name the three pieces that make a native app host Flutter, and state who owns the app lifecycle in an add-to-app setup.
Show answer
The three pieces are the Flutter module (the Dart project built with the module template), the Flutter engine (the runtime that runs Dart and renders), and a view host (FlutterActivity or FlutterFragment on Android, FlutterViewController on iOS). The native host app owns the app lifecycle, not Flutter.