Sub topic 6
Passing Initial Data with Creation Parameters
Prerequisites and the data path
This chapter assumes that you know basic Flutter and Dart, can read simple Kotlin and Swift, and understand the basic idea of a MethodChannel. You should also know that a platform view is a native Android or iOS view displayed inside Flutter's widget tree.
Creation parameters are the values sent from Dart when Flutter creates a native view. They are useful for initial configuration, such as:
- The text shown by a native label.
- A URL loaded by a native web view.
- A map configuration for a native map.
- An identifier for a native media or payment component.
The data crosses the boundary in three steps:
The Dart widget and native registration must use the same view type string. The codec must also be compatible on both sides.
Sending parameters from Dart
AndroidView and UiKitView both accept creationParams and creationParamsCodec. StandardMessageCodec supports common values such as strings, numbers, booleans, lists, and maps.
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};
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return AndroidView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: params,
creationParamsCodec: const StandardMessageCodec(),
);
case TargetPlatform.iOS:
return UiKitView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: params,
creationParamsCodec: const StandardMessageCodec(),
);
default:
return const Text('Unsupported platform');
}
}
}The parameters are sent only during creation. If text changes later, Flutter does not automatically update the existing native label. Use a method channel or another communication mechanism for later changes.
Reading parameters on Android
The Android factory receives the decoded value as args. With StandardMessageCodec, a Dart map arrives as a map-like object. Read values defensively because the native side cannot assume that Dart supplied the expected type.
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 factory's codec must match the codec used by AndroidView. Register the factory under the same view type:
flutterEngine.platformViewsController.registry.registerViewFactory(
"com.example.app/native_label",
NativeLabelFactory()
)A missing key, an unexpected type, or a misspelled view type usually produces an empty view or a runtime error. Defaults and validation make failures easier to diagnose.
Reading parameters on iOS
The iOS factory receives the decoded arguments in args. A Dart map becomes a Swift dictionary when it uses FlutterStandardMessageCodec.
class NativeLabel: NSObject, FlutterPlatformView {
private let label: UILabel
init(frame: CGRect, arguments args: Any?) {
label = UILabel(frame: frame)
if let params = args as? [String: Any],
let text = params["text"] as? String {
label.text = text
} else {
label.text = ""
}
label.font = UIFont.systemFont(ofSize: 20)
super.init()
}
func view() -> UIView {
return label
}
}The factory should return the same codec through createArgsCodec(). It must also be registered with the exact same view type string as the Dart widget.
Codec and schema decisions
Treat the parameter map as a small schema: a documented set of keys and value types. Keep it stable and avoid sending native objects that the standard codec cannot represent.
| Dart value | Common native value |
|---|---|
| `String` | Kotlin `String`, Swift `String` |
| `int` or `double` | Kotlin number, Swift number |
| `bool` | Kotlin `Boolean`, Swift `Bool` |
| `Map<String, dynamic>` | Kotlin map, Swift dictionary |
| `List<dynamic>` | Kotlin list, Swift array |
Exercise
A Dart widget sends {'count': 3} with StandardMessageCodec, but Android reads params["count"] as String. What should change?
Show answer
Android should read the value as a number, such as params?.get("count") as? Int, or Dart should send a string if text is the intended schema. Both sides must agree on the key and type.