π
Platform Channels
Calling Native Code (Swift/Kotlin) from Flutter
Flutter handles most UI with its own rendering engine, but platform-specific features (camera, sensors, payments) require native code.
MethodChannel exchanges asynchronous messages between Dart and native. When Dart calls invokeMethod(), iOS (Swift/ObjC) or Android (Kotlin/Java) handlers execute.
In most cases, pre-built pub.dev packages (camera, geolocator, etc.) exist, so you rarely need to write your own.
Implementation Steps
1
Dart side: Create MethodChannel + call invokeMethod()
2
iOS side: Register FlutterMethodChannel handler in AppDelegate (Swift)
3
Android side: Register MethodChannel handler in MainActivity (Kotlin)
4
Error handling: Handle PlatformException + platform-specific branching
Pros
- ✓ 100% access to native features
- ✓ Reuse existing native code assets
Cons
- ✗ Must write native code for both iOS and Android separately
- ✗ Additional Swift/Kotlin knowledge required
Use Cases
Using existing native SDK in Flutter app
Directly implementing platform features not on pub.dev