Platform Channels & Native Integration in Flutter

Call Kotlin and Swift from Dart with MethodChannel, EventChannel and Pigeon, and embed native views.

Advanced · about 6 hours · difficulty 4/5

Learn first: Structuring Real Flutter Apps

Flutter plugins on pub.dev cover most device features, but sometimes you need custom native code.

Channels

  • MethodChannel: call a named method on the native side and get a result asynchronously (await channel.invokeMethod("getBattery")).
  • EventChannel: receive a stream of events from native code, such as sensor updates.
  • Pigeon: generates type-safe Dart, Kotlin and Swift code from a Dart interface definition. Prefer it over hand-written string-based channels.

Native views

AndroidView and UiKitView (platform views) embed native UI such as maps or ad views inside the widget tree. They cost some performance, so use them only when needed.

FFI

dart:ffi calls C libraries directly, useful for performance-critical or existing C/C++ code.

Good practice

  • Wrap channels in a Dart class so the rest of the app never sees method names or raw maps.
  • Handle PlatformException and missing implementations on each platform.
  • If it could be reused, package it as a plugin.

Good enough to move on: you wrote a Pigeon-based API that returns device information from both Android and iOS.

Key concepts

  • MethodChannel
  • EventChannel
  • Pigeon code generation
  • Platform views
  • dart:ffi
  • Writing a plugin package

Mini project

Create a Flutter plugin that streams the device's step counter from native code to Dart and displays it live.

Check yourself

Why is Pigeon safer than a hand-written MethodChannel?

Free resources