Sub topic 11
Performance costs and known limitations
This chapter assumes you can already register a platform view on Android and iOS. It also assumes you can show it with AndroidView or UiKitView, and that you have a basic feel for Flutter's rendering model (UI thread, raster thread, layers). Here you learn what a platform view costs, why, and which restrictions to plan around.
Why platform views cost more than Flutter widgets
Flutter draws every pixel of a normal widget itself. The engine takes the layer tree, rasterizes it on the raster thread, and hands one finished frame to the operating system. A native view does not fit this model. Android or iOS owns it, and the system UI toolkit draws it. Flutter has to place that view in the same visual stack as its own pixels, and the two sides must stay in sync every frame.
Three kinds of cost follow from this:
- Memory. A native view brings its own view hierarchy, and some composition modes add an extra rendering surface the size of the view.
- Synchronization. Flutter content and native content must show the same moment in time. When a platform view moves, scrolls or resizes, both sides have to agree on its position before the frame is shown.
- Lost optimizations. Some effects that Flutter applies cheaply to its own layers cannot be applied to a native view.
A single static platform view on a modern device is often fine. The trouble starts with many views, views inside scrolling lists, or animation over or around them.
Android composition modes and what they cost
On Android, Flutter has three ways to put a native view on screen. The previous chapter covered how to select them. This table compares what you pay for each.
| Mode | How it works | Main cost | Main limitation |
|---|---|---|---|
| Virtual display (VD) | The native view renders into an off-screen virtual display. Flutter reads that surface as a texture. | Extra surface memory. Touch and keyboard events are forwarded by Flutter, not delivered natively. | Text input, accessibility and some views behave oddly. Views with their own `SurfaceView` are a poor fit. |
| Hybrid composition (HC) | The native view is a real view in the Android hierarchy. Flutter content is composited around it. | Frame synchronization between threads. On older Android versions, Flutter's own rendering can slow down while the view is present. | The most faithful behavior, but the slowest for Flutter's own content. |
| Texture layer hybrid composition (TLHC) | The native view is a real view, but its pixels are drawn into a texture that Flutter composes. | Cheaper than HC for Flutter content, and scrolling and animation are smooth. | Views that draw to a `SurfaceView` or `TextureView` inside cannot be captured this way. Flutter then falls back to another mode. |
In current Flutter releases, AndroidView picks TLHC when the native view supports it and falls back otherwise. The PlatformViewLink route from the previous chapter lets you ask for a specific mode. initSurfaceAndroidView requests the TLHC path. initExpensiveAndroidView requests full hybrid composition. The name of the second method is a hint: it exists for cases where fidelity matters more than speed.
Which modes are available, and how they fall back, has changed between Flutter versions. Check the platform views page in the Flutter docs for your SDK version before you rely on a specific mode.
iOS costs
iOS has one mechanism, and it behaves like hybrid composition: the native UIView is inserted into the real view hierarchy and Flutter content is layered around it. Flutter has to interleave its own layers with the native view, so a frame that contains a platform view takes more work than one without it. Older engine versions also merged the raster thread with the platform (main) thread in frames that contained a platform view. This is another reason a heavy UIView can cause jank in Flutter content around it.
Treat the exact threading behavior as version-dependent. Treat the direction as stable: more platform views and more native work per frame mean a higher risk of dropped frames.
Measure before you decide
Do not guess. Performance numbers for platform views depend on the device, the Android version, the native view and the Flutter version.
- Run the app in profile mode on a real device, ideally an older one. Emulators and debug builds give misleading numbers.
- Open the performance view in Flutter DevTools and scroll or animate with the platform view on screen.
- Compare frame times with the platform view and with a placeholder of the same size.
- Watch both the UI and the raster timelines. A slow native view can hurt either.
Test the case users will actually hit. For example, test a map in a scrolling feed, not a map alone on a blank screen.
Reducing the cost in practice
The cheapest platform view is the one that does not exist. These habits help most:
- Create late. Show a lightweight Flutter placeholder, such as a static image or a button, and create the native view only when the user asks for it.
- Destroy early. Remove the widget from the tree when the user leaves the screen, so the native view is disposed instead of kept alive.
- Use few. One large view is cheaper than several small ones. Avoid platform views in long
ListViewitems. - Keep layout simple. Give the view fixed, bounded constraints. Avoid animating its size or position.
- Keep Flutter effects off it. Do not wrap a platform view in opacity animations, shaders or filters unless you have tested the result.
The next example implements the first two points. It shows a placeholder until the user taps a button, and it removes the native view again on request. It reuses the NativeLabel widget from the Dart-side chapter, assumed to live in native_label.dart.
import 'package:flutter/material.dart';
import 'native_label.dart';
class OnDemandNativeView extends StatefulWidget {
const OnDemandNativeView({
super.key,
required this.nativeBuilder,
this.height = 200,
});
final WidgetBuilder nativeBuilder;
final double height;
@override
State<OnDemandNativeView> createState() => _OnDemandNativeViewState();
}
class _OnDemandNativeViewState extends State<OnDemandNativeView> {
bool _active = false;
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: widget.height,
child: _active
? widget.nativeBuilder(context)
: const Center(child: Text('Native view not loaded')),
),
_active
? TextButton(
onPressed: () => setState(() => _active = false),
child: const Text('Release native view'),
)
: FilledButton(
onPressed: () => setState(() => _active = true),
child: const Text('Load native view'),
),
],
);
}
}
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: Center(
child: OnDemandNativeView(
nativeBuilder: (context) => const NativeLabel(text: 'Loaded on demand'),
),
),
),
),
);
}When _active becomes false, the AndroidView or UiKitView leaves the tree. Flutter then disposes the native view, and your dispose() implementation runs on the Android side. That is the lifecycle hook from the earlier chapter.
Known limitations
Performance is only half of the picture. Platform views also have functional limits. Check them before you commit to a design.
Visual effects and compositing
- The native view is not part of Flutter's layer tree in the normal sense. Widgets such as
ShaderMaskandColorFiltereddo not apply to it on iOS.BackdropFilterhas limited support. Treat any effect that has to read the pixels of the view as unreliable. - Flutter widgets can be drawn above a native view, but on some modes this adds layers and cost. Do not stack many Flutter overlays on top of a platform view.
- Clipping and transforms may behave differently from the same operations on Flutter widgets. Rounded corners are the usual example. Test them.
Capturing and testing
RepaintBoundary.toImageand similar screenshot tools capture Flutter pixels, not the native view. Expect a blank area where the platform view is.- Widget tests do not run native code. A test that pumps an
AndroidViewhas no native side to answer it. Wrap the platform view in your own widget and replace that wrapper in tests with a fake. - Golden tests cannot show the native content for the same reason.
Platform coverage
AndroidViewandUiKitViewexist only for Android and iOS. Web and desktop have their own, separate mechanisms. Thedefaultbranch in theNativeLabelexample from earlier chapters returns a fallback widget for this reason. Always provide one.- Behavior differs between the two mobile platforms, so budget separate testing time for each.
Input, focus and accessibility
- Keyboard focus and text input between Flutter and a native view can behave differently from either side alone. This is most visible in virtual display mode.
- Accessibility depends on the native view exposing its own accessibility information. Flutter does not generate it for you. Test with TalkBack and VoiceOver.
- Gesture conflicts are common when a platform view sits inside a scrollable parent. The gesture recognizer chapter covers this.
Choosing: platform view or something else
The decision starts from the question of what you actually need from the native side.
In short:
- Pure Flutter is the cheapest and the most portable choice. Prefer it whenever the feature can be rebuilt in Dart.
- Texture fits sources of frames, such as camera previews or video. The native side renders into a texture, and Flutter displays it with a
Texturewidget. You lose native touch handling and native view behavior, but you avoid most composition costs. - Platform view is for components that must stay real native views, such as maps, web views and native ad views. These need native interaction, native text input, or a vendor SDK that only provides a view.
Common mistakes
- Judging performance in debug mode or on an emulator.
- Putting a platform view in every row of a list.
- Keeping a hidden platform view in the tree, for example inside an
IndexedStack. The native view stays alive, and so do its memory and its callbacks. - Assuming the Android result also holds on iOS.
- Forgetting a fallback widget for unsupported platforms.
- Writing tests that expect the native content to appear.
Exercise
A product page needs a map at the top, followed by a long scrolling list of reviews. Describe two changes that reduce the performance cost of the map's platform view, and say how you would check that they helped.
Show answer
First, create the platform view late: show a static map image as a placeholder and swap in the interactive native map only when the user taps it, then remove it when they leave. Second, keep it to a single view with a fixed height instead of repeating it in list items, and avoid animating its size or opacity. To check, run in profile mode on a real, older device, scroll the reviews with DevTools open, and compare UI and raster frame times with the platform view against the same screen using a placeholder of the same size.
Exercise
You need to display a live camera preview that your native code already renders to frames. Should you use a platform view or a Texture, and what do you give up with your choice?
Show answer
Use a Texture, because the native side only produces frames and Flutter can display them without hosting a real native view, which avoids most composition and synchronization costs. You give up native touch handling, native view behavior and any native controls on the preview, so any interaction has to be built in Flutter.