Sub topic 2
How a View Type Connects Dart to Native Code
This chapter assumes you know basic Flutter widgets and Dart, can read simple Kotlin or Swift, and understand the basic idea of a MethodChannel. A platform view is a native Android or iOS view that Flutter places inside its widget tree. The connection depends on one shared identifier called a view type.
The platform view architecture
A platform view crosses several layers:
- Dart widget: Requests a native view by its view type.
- View registry: Stores native factories under view type strings.
- Native factory: Creates one native view instance when Flutter requests it.
- Native view: The Android
Viewor iOSUIViewdisplayed on screen.
The view type is not a class name. It is a string contract. Dart and both native platforms must use the same value, including capitalization and punctuation.
Flutter does not ask Android or iOS to create a view by guessing its class. It looks up the requested string in the registry. If no factory has been registered for that string, Flutter cannot create the view.
A useful mental model is a name-to-constructor map:
| Platform | Registration key | Factory result |
|---|---|---|
| Android | `com.example.app/native_label` | `PlatformView` wrapping a `TextView` |
| iOS | `com.example.app/native_label` | `FlutterPlatformView` wrapping a `UILabel` |
The Dart side requests a type
The Dart widget selects the platform-specific widget but keeps the same view type:
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 viewType = 'com.example.app/native_label';
@override
Widget build(BuildContext context) {
final params = <String, dynamic>{'text': text};
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: viewType,
creationParams: params,
creationParamsCodec: const StandardMessageCodec(),
);
}
if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: viewType,
creationParams: params,
creationParamsCodec: const StandardMessageCodec(),
);
}
return const Text('Native view is not supported');
}
}AndroidView and UiKitView are Flutter widgets. They do not create the native object themselves. They send the view type to the platform view system. The creationParams map is optional initial data. Both sides must use compatible codecs to encode and decode it.
The native registry resolves the type
On Android, register a PlatformViewFactory with the Flutter engine:
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
flutterEngine.platformViewsController.registry.registerViewFactory(
"com.example.app/native_label",
NativeLabelFactory()
)
}
}NativeLabelFactory must implement the creation logic. Flutter calls it when Dart requests the matching type. The factory can create many instances, so it receives a viewId that uniquely identifies the new instance.
On iOS, registration uses a FlutterPlatformViewFactory and a plugin registrar:
let registrar = self.registrar(forPlugin: "NativeLabelPlugin")!
registrar.register(
NativeLabelFactory(),
withId: "com.example.app/native_label"
)The iOS factory's create method returns a new object conforming to FlutterPlatformView. Its view() method returns the actual UIView that Flutter embeds.
Common errors and boundaries
The most common failure is a mismatched view type. A spelling difference produces an error even if the native factory exists. Define the string once per platform, or place it in a shared, carefully maintained contract.
Register the factory before the Dart widget is built. Registering it after the first request is too late for that view instance.
A factory creates the view. It does not automatically keep Dart and native state synchronized after creation. For later updates, use a per-view channel or another explicit communication design.
The registry is also engine-specific. If an application creates more than one Flutter engine, register the factory with every engine that can display the view.
>callout note > Treat the view type as a public interface between three implementations: Dart, Android, and iOS. Change it only when you intentionally change that interface.
Exercise
Dart requests com.example.app/native_label, but Android registered com.example.app/nativeLabel. What happens, and what is the fix?
Show answer
The registry does not find a matching factory, so the platform view cannot be created. Make the strings exactly identical on both sides.