Sub topic 15
Designing a Maintainable Platform Integration
Assume you know intermediate Dart and Flutter, including Future, Stream, asynchronous code, and basic Kotlin or Swift. This chapter focuses on structure. The goal is to keep platform-channel details out of widgets and make the Android and iOS implementations follow the same contract.
Put a Boundary Around Each Channel
A platform channel is an integration boundary. Treat it like an API with a stable contract:
- Keep channel names and method names in one place.
- Put
MethodChannelandEventChannelcalls in a Dart service or repository. - Expose typed Dart methods and streams instead of raw channel values.
- Validate arguments before sending them to native code.
- Keep widgets unaware of channel names, codec details, and platform checks.
- Make Android and iOS implement the same operations and error meanings.
A wrapper converts an untyped platform message into an application-level API:
import 'package:flutter/services.dart';
abstract interface class DeviceStatus {
Future<bool> isEnabled();
Stream<double> get temperature;
}
final class ChannelDeviceStatus implements DeviceStatus {
static const _methods = MethodChannel('com.example.app/device_status');
static const _events = EventChannel('com.example.app/temperature');
@override
Future<bool> isEnabled() async {
final value = await _methods.invokeMethod<bool>('isEnabled');
if (value == null) {
throw StateError('Native code returned no enabled value');
}
return value;
}
@override
Stream<double> get temperature => _events
.receiveBroadcastStream()
.map((value) => (value as num).toDouble());
}The abstract interface makes the contract explicit. A fake implementation can replace ChannelDeviceStatus in unit tests, while production code uses the channel implementation.
Define a Shared Contract
Before writing platform code, document each operation and its data shape.
| Operation | Direction | Data contract |
|---|---|---|
| `isEnabled` | Dart to native | No arguments; returns a Boolean |
| `temperature` | Native to Dart | Stream of numeric values |
Android and iOS may use different operating-system APIs, but they should return equivalent results. Do not expose platform-specific types such as an Android Intent or an iOS object through the Dart API. Convert them to supported, stable values at the boundary.
Keep Lifecycle Rules Visible
A service that opens a native subscription must define when it starts and stops. EventChannel cancellation should release sensors, listeners, timers, or callbacks. If multiple Dart consumers are possible, decide whether the native resource is shared or created per subscription.
Register handlers when the Flutter engine is configured, and unregister or detach resources when the engine or host component is destroyed. Native callbacks should deliver events on the thread expected by Flutter. Move blocking work to an appropriate background mechanism rather than blocking the platform channel handler.
Exercise
Exercise
Where should a Flutter widget read a native sensor stream: directly from an EventChannel or through a typed service?
Show answer
Through a typed service. The service owns channel details, converts values, and gives widgets a stable Dart API.