Communicate Between Flutter and Native Android and iOS Code Using Platform Channel | by Sumit Ghosh | Feb, 2022

How to use Platform channel in Flutter

Person on laptop
Photo by Windows on Unsplash
Flutter app client tied to the method channel which goes to the iOS host and Android host.
Photo by Flutter official docs
static const platformChannel = MethodChannel('test.flutter.methodchannel/iOS');
  1. The platform might throw some exception so in order to be safe we ​​will fall back to the catch block.
    Note: In the case of some platform-specific exception, it throws PlatformException.
  2. We do setState which will rebuild the UI to show the device model.
  • Go to the ios folder, if your project name is simple_platform_channelthen simple_platform_channel > ios .
  • In the iOS folder open runner.xcworkspace
Runner > Runner > AppDelegate
  1. Why are we creating a method channel again?
    As we are trying to have an interaction between two frameworks the connections should be established on both sides.
    Note:
    – In order to establish a connection, we need to have the same channel name as we have defined in the Flutter.
    binaryMessenger. We are using the default binary messenger provided by FlutterViewController. This is why we pulled out the FlutterViewController.
  2. Here we are preparing the method handler. It will handle any method call invoked on the method channel.
  3. We are attaching a method call back handler, and passing a closure (in case you are wondering what’s that, it’s just an anonymous function. If you’ve used List.forEach in flutter, then you know it already).
    There are two parameters to the anonymous function:
    – call ( FlutterMethodCall) — It provides the details of the method call, like method name and its arguments.
    – result (FlutterResult) — This is the result you will return back to the flutter module, it’s denoted as the @escaping which means it’s an async call, so you can perform some time-taking operation and then call the result.
  4. Just checking if the invoked method name is getDeviceModel. Here it doesn’t make much sense to check, but when there are multiple methods you need to check to distinguish between them.
  5. Calling another method to get the device Model.
  6. We have used the iOS device API to get the result.
  7. Calling the result with the data, which helps in passing this back to the Flutter module.
  8. If we invoke some method from the Flutter side which is not implemented in native we throw FlutterMethodNotImplemented.
platformChannel.invokeMethod('getDeviceModel',
{"flutterAppVersion": "0.0.1", "developerName": "XYZ"});
result( ------ )
1. Future<T?> invokeMethod<T>(String method, [ dynamic arguments ])
2. Future<Map<K, V>?> invokeMapMethod<K, V>(String method, [ dynamic arguments ])
3. Future<List<T>?> invokeListMethod<T>(String method, [ dynamic arguments ])

In five simple steps, you can run a platform-specific code in Flutter:

  1. Invoke a method on the channel and await the result.
  2. Create a FlutterMethodChannel in iOS, in the didFinishLaunchingWithOptions.
  3. Set the callback handler on the channel.
  4. Use the native APIs to perform some action and return the result.

Some useful resources on Platform channel:

Leave a Comment