Kotlin

Flutter Method Channels: Communicating with Native Android Code

By Pawan Kumawat • Published Nov 30, 2025 • 11 min read

1. Understanding Platform Channels

Flutter uses an asynchronous message passing architecture called **Platform Channels** to communicate between Dart code and native host code (Kotlin/Java on Android, Swift/Objective-C on iOS).

2. Dart MethodChannel Invocation

In Dart, instantiate a `MethodChannel` with a unique domain string name, then call `invokeMethod`:

device_info.dart
import 'package:flutter/services.dart';

class NativeDeviceInfo {
  static const _channel = MethodChannel('online.pawankumawat/device_info');

  static Future<int> getBatteryLevel() async {
    try {
      final int result = await _channel.invokeMethod('getBatteryLevel');
      return result;
    } on PlatformException catch (e) {
      print("Failed to get battery level: ${e.message}");
      return -1;
    }
  }
}

3. Native Kotlin Handler

In your Android project's `MainActivity.kt`, override `configureFlutterEngine` to process incoming MethodChannel calls:

MainActivity.kt
package com.example.app

import android.content.Context
import android.os.BatteryManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
    private val CHANNEL = "online.pawankumawat/device_info"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "getBatteryLevel") {
                val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
                val batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)

                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else {
                result.notImplemented()
            }
        }
    }
}

4. Conclusion

Method channels empower Flutter developers to leverage native Android SDK features directly when open-source plugins fall short.

← Back to Blog Index