Flutter Platform Channels & Dart FFI

“Single codebase, multiple platforms” is perhaps the biggest advantage of Flutter.

But sometimes, we need to use platform-specific APIs to access features such as camera input, geolocation, connectivity, and notifications.

Luckily, there are many packages that can do the heavy lifting for you so that you don’t have to write platform-specific code by hand.

But it’s still very useful (and interesting!) to understand how Flutter apps communicate with the underlying platform APIs.

Platform Channels

A good place to start is this page that shows how to use platform channels to read the battery level on iOS and Android:

There is also a more detailed guide, which explains everything you need to know (including how to stream data through channels and how to test your platform channels code):

To write platform-specific code, you’ll need to become familiar with Swift (on iOS) and Kotlin (on Android). This is a good thing because it makes you a better developer who is comfortable with more than just one language or framework.

You don’t need to become an expert in other languages. But knowing just enough to find your way around is a valuable skill. And since many of the existing plugins are far from perfect, sooner or later, you may find yourself working with platform-specific APIs.

Dart FFI

Platform channels are great for accessing platform-specific APIs.

But what if you want to run performance-critical code that is written with a different language, such as C, C++ or Rust? Or what if you want to use machine learning libraries written in Python?

For this, Dart defines a foreign function interface, and this tutorial shows how to read the device battery using Dart FFI:

Daily Challenge - FFI Tutorial

Today’s challenge is to follow this tutorial:

It is quite long, but it will give you a much better understanding of how things work on the iOS and Android side.

Happy coding!

Questions? Let's chat