Sub topic 5
Showing the Native View with `AndroidView` and `UiKitView`
This chapter assumes you know basic Dart and Flutter widgets, and that the native Android and iOS factories are already registered. The registration connects a native factory to a shared view type string. This chapter focuses on placing that native view in the Flutter widget tree.
Choose the Flutter widget by platform
Flutter provides two main platform-view widgets:
AndroidViewdisplays a native AndroidView.UiKitViewdisplays a native iOSUIView.
Both widgets need the same viewType value that the native side used during registration. Flutter sends this value across the platform boundary to find the correct factory.
A common pattern is to select the widget with defaultTargetPlatform:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class NativeLabel extends StatelessWidget {
const NativeLabel({
super.key,
required this.text,
});
final String text;
static const String viewType = 'com.example.app/native_label';
@override
Widget build(BuildContext context) {
final creationParams = <String, dynamic>{
'text': text,
};
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return AndroidView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
case TargetPlatform.iOS:
return UiKitView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
default:
return const Text(
'Native view is not supported on this platform',
);
}
}
}
class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: SizedBox(
width: 300,
height: 80,
child: NativeLabel(text: 'Rendered by native code'),
),
),
);
}
}The SizedBox is important. A platform view participates in Flutter layout, so it needs finite constraints. Without a height or another constraint, the view may receive an invalid or unexpectedly small size.
How the widget connects to native code
The connection uses the same identifier on three sides:
| Part | Example value | Purpose |
|---|---|---|
| Dart `viewType` | `com.example.app/native_label` | Requests a native view |
| Android registry | Same string | Finds the Android factory |
| iOS registrar | Same string | Finds the iOS factory |
The string has no special required format. A reverse-domain name reduces the chance of collisions with another plugin or feature.
When Flutter builds AndroidView or UiKitView, it asks the platform to create an instance of that view type. The registered factory creates the native object and returns it to Flutter. Each widget instance receives its own native view instance.
Pass initial data with creation parameters
creationParams contains values needed when the native view is first created. StandardMessageCodec supports common values such as strings, numbers, booleans, lists, and maps.
The Dart map must match the native code's expected keys and types. For example, Android might read params["text"], while Swift might read params["text"] as? String.
Creation parameters are initial configuration. If the value changes later, rebuilding the widget does not provide a general update protocol for an already-created native view. Use a method channel or another explicit communication mechanism for later changes.
>callout tip > Keep the view type string and creation-parameter keys in one documented contract. A spelling difference between Dart and native code usually results in an empty view or a runtime error. >end
Layout, platform checks, and unsupported targets
defaultTargetPlatform is useful when the widget should follow Flutter's target platform. It is different from checking whether a platform is physically available at runtime, so test the behavior on real Android and iOS targets.
For web, Windows, macOS, and Linux, return a Flutter fallback or provide separate implementations. Do not instantiate AndroidView or UiKitView on unsupported platforms.
The native view also has its own rendering and accessibility behavior. Give it enough space, test text scaling, and verify that its appearance fits the surrounding Flutter layout.
Exercise
What must match between the Dart widget and the native registration, and why should the widget usually have explicit size constraints?
Show answer
The Dart viewType must match the identifier used to register the native factory, so Flutter can find the correct implementation. Explicit constraints give the embedded native view a definite size during Flutter layout.