Platformkanalen: Integratie van Swift en Kotlin Native Code
Flutter is geweldig voor 90% van de app-functies, maar ze bestaan gevallen waarin u directe toegang tot de native API's van het platform nodig heeft: biometrische sensoren, Bluetooth LE, Apple Pay, Google Pay, toegang tot bestandssysteem native, integratie met SDK's van derden niet beschikbaar als Flutter-plug-ins. Voor deze gevallen zijn er Platformkanalen.
Platformkanalen creëren een tweerichtingscommunicatiebrug tussen Darts en native code (Swift op iOS, Kotlin op Android). Je schrijft geen Java o Legacy Objective-C: Modern Kotlin en Swift, met coroutines en async/await respectievelijk. In deze handleiding bouwen we een complete plug-in die toegang geeft tot naar de nabijheidssensor van het apparaat - niet beschikbaar in Flutter core.
Wat je gaat leren
- MethodChannel: Dart → native oproepen met antwoord (verzoek/antwoord)
- EventChannel: continue gegevensstroom van native → Dart
- BasicMessageChannel: ongestructureerde bidirectionele communicatie
- Foutafhandeling: PlatformException vanuit beide richtingen
- Threading: hoofdthread, achtergrondthread, FlutterEngine
- Plug-inpakket: structuur voor herbruikbare plug-ins
- Pigeon: Typeveilige codegeneratie voor platformkanalen
MethodeKanaal: Synchrone aanvraag/antwoord
Il MethodeKanaal en het meest voorkomende type kanaal: de Dartcode roept een native methode aan en wacht op een antwoord. Ideaal voor eenmalige operaties hoe u informatie over het apparaat kunt krijgen, een native scherm kunt openen, een biometrische operatie uitvoeren.
// lib/proximity_sensor.dart: interfaccia Dart
import 'package:flutter/services.dart';
class ProximitySensor {
static const _channel = MethodChannel('com.example.proximity_sensor');
// Verifica se il sensore e disponibile
static Future<bool> isAvailable() async {
try {
final bool result = await _channel.invokeMethod('isAvailable');
return result;
} on PlatformException catch (e) {
// PlatformException: errore dal codice nativo
debugPrint('ProximitySensor.isAvailable error: ${e.code} - ${e.message}');
return false;
} on MissingPluginException {
// Plugin non registrato (es. desktop non supporta il sensore)
return false;
}
}
// Ottieni la distanza attuale (in cm, 0 = vicino, null = non disponibile)
static Future<double?> getDistance() async {
try {
final double? distance = await _channel.invokeMethod<double>('getDistance');
return distance;
} on PlatformException catch (e) {
throw ProximitySensorException(e.code, e.message ?? 'Unknown error');
}
}
}
class ProximitySensorException implements Exception {
final String code;
final String message;
const ProximitySensorException(this.code, this.message);
@override
String toString() => 'ProximitySensorException($code): $message';
}
// android/src/main/kotlin/.../ProximitySensorPlugin.kt
// Implementazione Android con Kotlin
package com.example.proximity_sensor
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
class ProximitySensorPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
private lateinit var channel: MethodChannel
private lateinit var context: Context
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
context = binding.applicationContext
channel = MethodChannel(
binding.binaryMessenger,
"com.example.proximity_sensor"
)
channel.setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"isAvailable" -> {
val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
result.success(sensor != null)
}
"getDistance" -> {
val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
if (sensor == null) {
// Errore: ritorna PlatformException a Dart
result.error(
"SENSOR_NOT_FOUND",
"Proximity sensor not available on this device",
null
)
return
}
// Leggi il valore attuale (semplificato: usa EventChannel per stream)
result.success(sensor.maximumRange.toDouble())
}
else -> result.notImplemented()
}
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
// ios/Classes/ProximitySensorPlugin.swift
// Implementazione iOS con Swift
import Flutter
import UIKit
public class ProximitySensorPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(
name: "com.example.proximity_sensor",
binaryMessenger: registrar.messenger()
)
let instance = ProximitySensorPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "isAvailable":
// iOS ha sempre il sensore di prossimita nei modelli standard
result(UIDevice.current.isProximityMonitoringEnabled ||
ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] == nil)
case "getDistance":
UIDevice.current.isProximityMonitoringEnabled = true
let isNear = UIDevice.current.proximityState
// Conversione in float: 0.0 = lontano, 1.0 = vicino
let distance: Double = isNear ? 0.0 : 10.0
result(distance)
default:
result(FlutterMethodNotImplemented)
}
}
}
EventChannel: continue gegevensstroom
L'Gebeurteniskanaal en ontworpen voor het streamen van data: sensoren die voortdurend worden bijgewerkt, geolocatiegebeurtenissen, hardwaremeldingen. De native zendt gebeurtenissen uit in een Dart-stream waar de widget naar kan luisteren StreamBuilder.
// Dart: EventChannel per stream del sensore di prossimita
class ProximitySensorStream {
static const _eventChannel = EventChannel('com.example.proximity_sensor/stream');
static Stream<double> get proximityStream {
return _eventChannel
.receiveBroadcastStream()
.map((event) => (event as double));
}
}
// Widget: StreamBuilder per visualizzare i dati in tempo reale
class ProximitySensorWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<double>(
stream: ProximitySensorStream.proximityStream,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Errore: ${snapshot.error}');
}
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
final distance = snapshot.data!;
return Column(
children: [
Text('Distanza: ${distance.toStringAsFixed(1)} cm'),
Icon(
distance < 5 ? Icons.phone_in_talk : Icons.phone_android,
size: 48,
color: distance < 5 ? Colors.red : Colors.green,
),
],
);
},
);
}
}
// Kotlin: EventChannel sul lato Android
class ProximitySensorStreamHandler(
private val context: Context
) : EventChannel.StreamHandler {
private var sensorManager: SensorManager? = null
private var sensorListener: SensorEventListener? = null
override fun onListen(arguments: Any?, events: EventChannel.EventSink) {
sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor = sensorManager?.getDefaultSensor(Sensor.TYPE_PROXIMITY)
if (sensor == null) {
events.error("SENSOR_NOT_FOUND", "No proximity sensor", null)
return
}
sensorListener = object : SensorEventListener {
override fun onSensorChanged(event: SensorEvent) {
// Invia ogni aggiornamento al Dart stream
events.success(event.values[0].toDouble())
}
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
}
sensorManager?.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_NORMAL)
}
override fun onCancel(arguments: Any?) {
// Pulisci le risorse quando lo stream viene cancellato
sensorManager?.unregisterListener(sensorListener)
sensorManager = null
sensorListener = null
}
}
Duif: Code Generatie Type-Safe
Duif en de officiële Flutter-tool om de code te genereren Platformkanalen boilerplate van een gedeelde Dart-definitie. Elimineert de noodzaak om handmatige serialisatie en garanties te beheren dat Dart en native code gesynchroniseerd zijn.
// pigeons/proximity.dart: definizione dell'interfaccia (source of truth)
import 'package:pigeon/pigeon.dart';
@ConfigurePigeon(PigeonOptions(
dartOut: 'lib/proximity_api.g.dart',
kotlinOut: 'android/src/main/kotlin/com/example/ProximityApi.g.kt',
swiftOut: 'ios/Classes/ProximityApi.g.swift',
))
// Messaggi (data classes)
class ProximityInfo {
final double distance;
final bool isNear;
const ProximityInfo({required this.distance, required this.isNear});
}
// HostApi: metodi implementati nel codice nativo, chiamati da Dart
@HostApi()
abstract class ProximityHostApi {
bool isAvailable();
ProximityInfo getCurrentReading();
}
// FlutterApi: metodi implementati in Dart, chiamati dal nativo
@FlutterApi()
abstract class ProximityFlutterApi {
void onProximityChanged(ProximityInfo info);
}
// Genera il codice:
// flutter pub run pigeon --input pigeons/proximity.dart
Wanneer moet u platformkanalen gebruiken versus bestaande plug-ins?
- Voordat u Platform Channels schrijft: zoek op pub.dev om te zien of er al een plug-in bestaat
- pub.dev heeft meer dan 40.000 pakketten: de meeste native API's zijn al gedekt
- Gebruik platformkanalen voor: bedrijfseigen SDK's, zeer specifieke API's, integratie met bestaande native legacy-code
- Voor nieuwe deelbare plug-ins: gebruik Pigeon voor typeveiligheid en minder onderhoud
Conclusies
Platformkanalen zijn het mechanisme dat Flutter echt universeel maakt: elke native API en bereikbaar met MethodChannel of EventChannel. Duif voegt typeveiligheid toe en verwijdert een groot deel van de standaardtekst. De sleutel tot goede native integratie en correct omgaan met fouten PlatformException en bronnen opschonen (luisteraar afmelden, stream annuleren) in de juiste levenscyclus.







