Sub topic 6
Implementing the API in Kotlin and Swift
Assume you know basic Flutter app structure, Dart Futures, and the Kotlin or Swift syntax shown here. This chapter starts after you have defined a Pigeon schema and generated its Dart, Kotlin, and Swift files.
Pigeon generates an interface for each native platform. Your code implements that interface, reads platform data, creates generated response objects, and registers the implementation with Flutter's binary messenger.
Implement the Kotlin API
Suppose the schema contains this host API:
@HostApi()
abstract class BatteryApi {
BatteryInfo getBatteryInfo();
}After generation, the Kotlin output contains a BatteryApi interface and a BatteryInfo data type. Implement the interface in a regular Kotlin class:
import android.content.Context
import android.os.BatteryManager
class BatteryApiImpl(
private val context: Context
) : BatteryApi {
override fun getBatteryInfo(): BatteryInfo {
val manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager
val level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
)
return BatteryInfo(
level = level.toLong(),
state = BatteryState.UNKNOWN
)
}
}The generated type mapping matters. A Dart int commonly becomes Kotlin Long, so the Android value must be converted with toLong(). The generated enum uses Kotlin enum values such as BatteryState.UNKNOWN.
Register the implementation when the Flutter engine is configured:
override fun configureFlutterEngine(
flutterEngine: FlutterEngine
) {
super.configureFlutterEngine(flutterEngine)
BatteryApi.setUp(
flutterEngine.dartExecutor.binaryMessenger,
BatteryApiImpl(this)
)
}setUp connects the generated message handler to your implementation. Without this call, Dart can create the generated client, but no native code receives the request.
Implement the Swift API
The generated Swift output contains a protocol with the same operation. Implement that protocol and return the generated response type:
import Flutter
import UIKit
final class BatteryApiImpl: BatteryApi {
func getBatteryInfo() throws -> BatteryInfo {
UIDevice.current.isBatteryMonitoringEnabled = true
let percentage = max(UIDevice.current.batteryLevel, 0)
let level = Int64(percentage * 100)
return BatteryInfo(
level: level,
state: .unknown
)
}
}Swift implementations can throw errors. Pigeon converts supported thrown errors into an error result that Dart receives as a failed Future.
Register the implementation after the Flutter view controller exists:
let controller = window?.rootViewController as! FlutterViewController
BatteryApiSetup.setUp(
binaryMessenger: controller.binaryMessenger,
api: BatteryApiImpl()
)The exact generated setup type can vary with the Pigeon version and output options. Use the generated file as the source of truth for the protocol and registration method names.
Compare the platform responsibilities
| Platform | Your code implements | Registration needs |
|---|---|---|
| Android | Generated Kotlin interface | Dart binary messenger and implementation |
| iOS | Generated Swift protocol | Flutter view controller's binary messenger |
Both implementations must honor the same schema. They should return equivalent units and meanings even when platform APIs differ. For example, Android and iOS should both report battery level from 0 through 100.
Check the registration path
When a call fails, check these points in order:
- The generated files were regenerated after the schema changed.
- The native class implements the generated interface or protocol exactly.
- The implementation is registered on the engine or binary messenger used by the running Flutter view.
- Native values match the generated types.
- The native method returns or throws according to the generated signature.
Exercise
Why can a generated Dart BatteryApi call compile successfully but still fail at runtime?
Show answer
The native implementation may not have been registered with BatteryApi.setUp or BatteryApiSetup.setUp. Code generation creates the client and handler types, but registration connects the handler to the running Flutter engine.