What is MethodChannel used for?
MethodChannel sends a method call to native code and receives a single response, like an RPC call.
Communicate between Flutter and native Android/iOS code
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Platform channels let Flutter communicate with native Android (Kotlin or Java) and iOS (Swift or Objective-C) code. They act as a bridge between the Dart side of your app and the platform-specific side, allowing you to call native APIs that are not available through existing Flutter plugins. Messages are passed asynchronously between the client (Flutter) and the host (native platform) using a channel name that both sides agree on. Flutter provides three types of channels for different use cases. MethodChannel handles one-time request and response calls, similar to an RPC call where Flutter asks for data and native code sends back a result. EventChannel provides a continuous stream of data flowing from native code to Flutter, which is ideal for sensor readings, location updates, or any real-time data feed. BasicMessageChannel sends simple messages back and forth using a codec for serialization. You can also use the Pigeon package to generate type-safe platform channel code that eliminates the risk of mismatched method names and data types between Dart and native code.
1// ── Flutter Side (Dart) ──2import 'package:flutter/services.dart';34class BatteryService {5static const platform = MethodChannel('com.myapp/battery');67Future<int> getBatteryLevel() async {8try {9final int level = await platform.invokeMethod('getBatteryLevel');10return level;11} on PlatformException catch (e) {12print('Failed: ${e.message}');13return -1;14}15}16}1718// ── Android Side (Kotlin) ──19// MainActivity.kt20// class MainActivity : FlutterActivity() {21// override fun configureFlutterEngine(flutterEngine: FlutterEngine) {22// super.configureFlutterEngine(flutterEngine)23// MethodChannel(flutterEngine.dartExecutor, "com.myapp/battery")24// .setMethodCallHandler { call, result ->25// if (call.method == "getBatteryLevel") {26// val level = getBatteryLevel()27// result.success(level)28// } else {29// result.notImplemented()30// }31// }32// }33// }3435// ── EventChannel for streams ──36static const eventChannel = EventChannel('com.myapp/sensorData');3738Stream<double> getSensorStream() {39return eventChannel.receiveBroadcastStream().map((event) => event as double);40}4142// Usage:43getSensorStream().listen((value) {44print('Sensor: $value');45});
MethodChannel is used for one-time request and response communication, similar to making an API call where you send a request and receive a single result back. EventChannel is used for continuous streams of data flowing from native code to Flutter, such as sensor readings, battery state changes, or GPS location updates. Use MethodChannel when you need a single value and EventChannel when you need ongoing data. Both require matching channel names on the Dart and native sides.
Treat every platform channel as a versioned contract:
Contract discipline avoids runtime surprises when native and Flutter teams iterate independently.
Communicate between Flutter and native Android/iOS code. Forms collect user input, and validation ensures the input is safe and useful.
Think in architecture, reliability, and team-level maintainability.
Show validation messages early and clearly, near the field that needs correction.
Most app trust problems start when invalid or incomplete input reaches backend systems.
Validated input at UI and domain layers protects business logic from invalid assumptions.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
MethodChannel sends a method call to native code and receives a single response, like an RPC call.
EventChannel provides a stream of events: perfect for sensor data, location updates, or any continuous data flow.
PlatformException is thrown when the native platform code returns an error.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue