Sub topic 3
Building and Registering the Android View
This chapter assumes you know basic Flutter and Dart widgets, can read simple Kotlin, and have used MethodChannel at a basic level. You should also be able to run a Flutter app on an Android emulator or device.
A Flutter platform view is a native Android View placed inside Flutter's widget tree. Use one when an Android SDK component already provides the behavior you need, such as a map, video player, web page, camera preview, or payment control. A pure Flutter widget is usually simpler and faster when it can provide the same result.
How Flutter finds the Android view
Flutter does not create the Android view from its Dart class directly. The two sides connect through a shared view type string.
The connection has three parts:
- Dart requests a view type, such as
com.example.app/native_label. - Android registers a
PlatformViewFactoryunder that exact string. - The factory creates a
PlatformViewfor each requested instance.
The registry is the lookup table between the Dart and Android sides.
A mismatch in the string prevents Flutter from finding the factory. Treat the string like a public identifier and keep it in one shared constant where practical.
Implementing `PlatformView`
The native wrapper implements PlatformView. Its getView method returns the actual Android View that Flutter should display. Its dispose method releases resources when Flutter removes the platform view.
This example wraps an Android TextView. The constructor receives initial data from Dart.
import android.content.Context
import android.view.View
import android.widget.TextView
import io.flutter.plugin.platform.PlatformView
class NativeLabel(
context: Context,
text: String
) : PlatformView {
private val textView = TextView(context).apply {
this.text = text
textSize = 20f
}
override fun getView(): View = textView
override fun dispose() {
// Stop listeners, close resources, or detach callbacks here.
}
}The context belongs to the Android activity. Pass it to the native view so the view can load resources, resolve themes, and create child views.
dispose may be empty for this small example, but real views often need cleanup. Camera sessions, media players, timers, listeners, and channel handlers can otherwise outlive the Flutter widget.
Creating views with a factory
Flutter creates a new native instance through a PlatformViewFactory. The factory receives a viewId and optional arguments. The viewId is unique for that platform-view instance. You can later use it to create an instance-specific communication channel.
StandardMessageCodec decodes common Dart values such as strings, numbers, lists, and maps.
import android.content.Context
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
class NativeLabelFactory :
PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(
context: Context,
viewId: Int,
args: Any?
): PlatformView {
val params = args as? Map<*, *>
val text = params?.get("text") as? String ?: ""
return NativeLabel(context, text)
}
}The cast to Map<*, *> is intentionally cautious. Dart data crosses a platform boundary, so native code should handle missing, unexpected, or incorrectly typed values without crashing.
Registering the factory
Register the factory while configuring the Flutter engine. The identifier must match the viewType used by Dart.
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
flutterEngine
.platformViewsController
.registry
.registerViewFactory(
"com.example.app/native_label",
NativeLabelFactory()
)
}
}The registration must happen before Dart builds the corresponding AndroidView. In a reusable Flutter plugin, register the factory from the plugin's Android registrar instead of tying it to one application activity.
| Component | Responsibility |
|---|---|
| `AndroidView` | Requests a native view from Dart |
| Registry | Finds the factory for a view type |
| `PlatformViewFactory` | Converts arguments into a native instance |
| `PlatformView` | Supplies and cleans up the Android `View` |
Checking the boundary
When the view does not appear, check the registration string first. Then verify that the factory receives the expected argument types and that the native view has a visible size. A platform view with no height can be created successfully but still appear invisible.
Exercise
What must be identical between the Dart widget and the Android registration for Flutter to create the native view?
Show answer
The view type string. Dart passes it to AndroidView, and Android uses the same string when registering the PlatformViewFactory.