Flutter is known for its cross-platform development magic — write once, run anywhere. But what happens when you need to access native platform features like GPS, Bluetooth, or background services that Flutter doesn’t support out of the box? That’s where platform-specific code comes in.
In this post, you’ll learn how to use platform channels to run native Android and iOS code from your Flutter app. Whether you're building production apps or exploring advanced capabilities, mastering platform channels gives you the best of both worlds — Flutter’s flexibility with native power.
Why Use Platform-Specific Code?
While Flutter has a rich ecosystem of plugins, sometimes you’ll need:
- Custom native features not supported by existing plugins.
- Native SDKs that can’t be used directly in Dart.
- Performance optimizations using native code.
Flutter handles this using platform channels, which let Dart code talk to Kotlin/Java (Android) and Swift/Objective-C (iOS).
How Platform Channels Work
Think of platform channels as a bridge between Flutter (Dart) and native platforms (Android/iOS). Flutter sends messages using a MethodChannel
, and the native side listens and responds.
Here’s how to set it up:
Step 1: Set Up MethodChannel in Flutter (Dart)
In your Dart code, define a method channel and write a method to call native code.
import 'package:flutter/services.dart';
class PlatformSpecific {
static const platform = MethodChannel('com.example.platform_specific');
Future<String> getNativeData() async {
try {
final String result = await platform.invokeMethod('getNativeData');
return result;
} catch (e) {
return "Failed to get data: $e";
}
}
}
Step 2: Android Native Code (Kotlin)
Open MainActivity.kt
(under android/app/src/main/kotlin/your/package/name/
) and handle the method channel.
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = "com.example.platform_specific"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call, result ->
if (call.method == "getNativeData") {
val data = "Hello from Android"
result.success(data)
} else {
result.notImplemented()
}
}
}
}
Step 3: iOS Native Code (Swift)
Open AppDelegate.swift
(under ios/Runner/
) and implement the channel handler.
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "com.example.platform_specific",
binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "getNativeData" {
result("Hello from iOS")
} else {
result(FlutterMethodNotImplemented)
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Step 4: Using the Native Method in Flutter UI
You can now call the platform-specific function from your Flutter widgets.
ElevatedButton(
onPressed: () async {
String message = await PlatformSpecific().getNativeData();
print(message); // Should print "Hello from Android" or "Hello from iOS"
},
child: Text("Get Native Message"),
)
Bonus: Sending Arguments and Returning Values
You’re not limited to just simple strings! You can pass maps, lists, and more to native code:
await platform.invokeMethod('calculateSum', {"a": 10, "b": 20});
Handle it on Android/iOS accordingly.
When Should You Use Platform Channels?
Use them when:
- No Flutter plugin exists for a native feature you need.
- You’re integrating third-party native SDKs.
- You want to optimize certain logic at the native level.
Conclusion
Platform channels are a powerful feature of Flutter that let you bridge the gap between Dart and native code. Once you understand the basics, you can extend your Flutter app to leverage any native capability available on Android or iOS.
Flutter gives you the best of both worlds — cross-platform development and full native control when you need it.
Got questions or want to see a real-life use case using the camera, GPS, or background services? Let me know in the comments or reach out!
ʀᴇᴍᴇᴍʙᴇʀ we ᴅᴇᴠᴇʟᴏᴘ Qᴜᴀʟɪᴛʏ, fast, and reliable websites and ᴀᴘᴘʟɪᴄᴀᴛɪᴏɴꜱ. Reach out to us for your Web and Technical services at:
☎️ +234 813 164 9219
Or...
🤳 wa.me/2347031382795
#Webfluxy #WebAppDev #WebTechnicalities #LearnWeb #AIAssisted #Programming #SoftwareEngineering