Skip to content
tezvyn:
Menu

Flutter Platform Channels: Talking to Native Code

Source: docs.flutter.devMediumHow cards are made

Platform Channels are a telephone line between your Dart code and native device APIs. Use them to access features like battery level or integrate a native SDK not available in Dart.

Why it exists

Flutter provides the UI, but apps often need to access device-specific hardware or OS services like the battery, GPS, or Bluetooth. Platform Channels were created to bridge the gap between the cross-platform Dart world and the native Android (Kotlin/Java) and iOS (Swift/Objective-C) worlds.

The mental model

Think of a Platform Channel as a dedicated, named message queue between your Flutter app and the host platform. Your Dart code puts a message on the queue (e.g., 'getBatteryLevel'), and your native code is listening. The native code picks up the message, performs the requested action using native APIs, and puts the result back on the queue for your Dart code to receive.

How it works

You define a MethodChannel with a unique string name in both your Dart and native code. In Dart, you use channel.invokeMethod('methodName', arguments) which returns a Future. On the native side, you set a MethodCallHandler that listens for incoming calls. When a call with a matching 'methodName' arrives, the handler executes the native logic and returns a result or an error, which completes the Future in your Dart code. Data is automatically serialized for supported types like numbers, strings, and maps.

When to use it

Use Platform Channels when you need to access a platform-specific API that has no Flutter plugin available, when you need to integrate a third-party native SDK (like a payment processor), or when you want to reuse a piece of complex, battle-tested native code within your Flutter application.

When not to use it

First, check pub.dev. If a well-maintained package already exists for your use case (like battery_plus or geolocator), use it. Don't reinvent the wheel. Also, for high-frequency, one-way data streams from native to Dart (like sensor data), an EventChannel is a more appropriate tool.

One canonical example

The 'hello world' for Platform Channels is fetching the device's battery level. This involves writing a small amount of native code on both Android and iOS to query the OS for the battery status and send the integer value back to a Future in Dart. This demonstrates the entire asynchronous request/response flow without complex logic.

Interview question

When is using a Flutter Platform Channel the most appropriate solution?

  • a.To create custom UI widgets that need to adapt their appearance based on the native platform.
  • b.To integrate a third-party native SDK that does not have an existing Flutter plugin.Correct
  • c.To replace all existing Flutter plugins with custom native implementations for better control.
  • d.To send continuous, high-volume sensor data from the native platform to Dart.
Why?

The card explicitly states that Platform Channels are used when there is no existing Flutter plugin for a native API or SDK. High-frequency, one-way data streams are better handled by an EventChannel, not a MethodChannel.

Just read this? Test yourself on what you have been reading.

Read the original → docs.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles