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:
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
MethodChanneland give it a name.
- Inside the
pickImage()method, we call
pickImageas a name, and
photosas the image source.
- We then parse the result, which will be either a
Stringrepresenting a file path, or a
FlutterErrorif something went wrong.
- Finally, we return a
Filewith 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
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.
- We register a
FlutterMethodChannel, using our
binaryMessenger. This is needed to communicate across the channel.
- In the
setup()method, we add our method call handler. We can use the
call.argumentsstrings as inputs to determine what action the Flutter app wants to take.
- In this case we recognize the
pickImagemethod, and determine the
sourceTypeto be either
- 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.
- 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
An alternative approach would be to decode the image into a byte array, and pass this along with the
scale across the channel. In this configuration, the Flutter app can then reconstruct the image via
Phew. That was quite a bit of code setup — most of it is boilerplate code to hook up the native image picker and saving to file.
Using platform channels is actually quite simple and boils down to this:
One final note for iOS: As our app accesses the camera, it is required to add a
NSCameraUsageDescription key to the
Info.plist file, as described on the Apple Docs.
What about Android?
Well, I’m no Android expert. But you know what?
Flutter already ships with an official image picker plugin. You can inspect the code and see how it’s done.
Here is the GitHub repo for this project. Enjoy!
image-picker-demo-flutter - Photos and Camera Image Picker built in Flutter
In this post I explained how platform channels work.
You can use them:
- to hook up your Flutter apps to platform-specific APIs
- as a stepping stone to build your own plugins
And before you roll your own, make sure to check for existing plugins on pub.dartlang.org first.
For more articles and video tutorials, check out Code With Andrea.
About me: I’m an iOS & Flutter developer, juggling between contract work, open source, side projects and blogging.