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.
We register a FlutterMethodChannel, using our flutterViewController as a binaryMessenger. 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.
In this case we recognize the pickImage method, and determine the sourceType to be either camera or photos.
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 Image.
An alternative approach would be to decode the image into a byte array, and pass this along with the width, height and scale across the channel. In this configuration, the Flutter app can then reconstruct the image via Image.memory.
Finally, our AppDelegate:
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.