Platform channels are the gateway to accessing platform-specific code in Flutter.

What do I mean by platform-specific? Here are some examples:

Access to device inputs (camera, microphone, location, bluetooth, motion sensors)

Secure storage (a.k.a. Keychain on iOS)

Platform channels are also the basic building block for creating plugins in Flutter.

And while the Dart Packages repository already has a lot of plugins ready to use, you may find yourself in this situation:

Need to fix a bug on an existing plugin and don’t have time to wait for the author to do it?

Need a specific platform feature for which there is no plugin yet?

Understanding platform channels will make your life easier.

Today I’ll show you how to use them to build an image picker, and hook it up to your Flutter app.

Before we dive in, let’s look at the anatomy of an app using platform channels:

Platform Channels diagram. Source: Flutter Docs

If we setup a MethodChannel to talk to the host application, we can access platform and 3rd party native APIs.

Sounds great! Time for an example:

Photos and Camera Image Picker

I’ll show you how to build this flow:

Let’s start by creating an ImagePickerChannel that we will use to interface with the native host:

This works as follows:

We create a MethodChannel and give it a name. Inside the pickImage() method, we call platform.invokeMethod() , passing pickImage as a name, and camera or photos as the image source. We then parse the result, which will be either a String representing a file path, or a FlutterError if something went wrong. Finally, we return a File with our image path (this can be rendered with Image.file ), or throw an error if anything went wrong.

Note that the result could be anything. In fact, the invokeMethod() call returns Future<dynamic> . In creating the channel for our image picker, we define a contract that requires the host app to return either String or FlutterError .

Time to jump over to the native code. Let’s take a look at how this works on iOS.

Implementing the image picker on iOS

We are going to need a few ingredients.

The first is a simple wrapper for UIImagePickerController , which specifies a sourceType and a completion handler:

Then, we’re going to create a FlutterChannelManager class. I’ll show you the code first, then explain it:

This works as follows:

We pass in a FlutterViewController . When building Flutter apps, the root view controller of the iOS app is always an instance of this class.

. When building Flutter apps, the root view controller of the iOS app is always an instance of this class. We register a FlutterMethodChannel , using our flutterViewController as a binaryMessenger . This is needed to communicate across the channel.

, using our as a . This is needed to communicate across the channel. In the setup() method, we add our method call handler. We can use the call.method and call.arguments strings as inputs to determine what action the Flutter app wants to take.

method, we add our method call handler. We can use the and strings as inputs to determine what action the Flutter app wants to take. In this case we recognize the pickImage method, and determine the sourceType to be either camera or photos .

method, and determine the to be either or . We then build and present our image picker, which either shows the photo gallery or the camera capture screen. This includes a check for isSourceTypeAvailable(.camera) as camera capture is not supported on the iOS Simulator.

as camera capture is not supported on the iOS Simulator. If an image is retrieved, it is saved to the temporary directory and a path to its file is returned.

On completion, we pass back the file path or any error to the channel.

Note how the image is saved to a file, and its file path is passed back as a result. As we have seen in the Flutter code, this is then used to retrieve the file and render it as an Image .