logo
logo
Sign in

Native Platform Support using Platform Channels

avatar
Expert App Devs
Native Platform Support using Platform Channels

So, How does native platform support be done by using Platform channels? We will read about it in this blog.

Basically, Flutter has its own supple system that allows users to call platform-specific APIs which might be in Java code on Android, or in Kotlin or in Swift or Objective-C code on iOS.


Dart code has no direct access to the platform-specific APIs of the Android SDK and iOS Cocoa Touch.


What are Platform Channels?


Platform channels are a simple mechanism that provides communication between the Dart code and the platform-specific code of a host app.


This means you can show a platform service in your host app code and have it invoked from the Dart end. Or vice versa.


It will be possible with plugins to create a Dart API backed by an iOS implementation which is written in Objective-C or Swift and an Android implementation which is written in Java or Kotlin and then it will be packaged up as a Flutter or Android or iOS triple stick together using platform channels. 


This means on the basis of how Flutter should use a particular platform API we can reuse, share, and distribute our take.


Platform channels API

  • Flutter communicates with platform code using asynchronous messages that are passed with binary messages. It means the message payload is a byte buffer. Since you have to receive and send string messages instead of byte buffers. This can be done using a message channel, which is a simple kind of platform channel, constructed with a string codec. Method channels utilize the standardized message “envelopes” to convey arguments and the method name from sender to receiver and to differentiate between successful and inaccurate results in the associated reply. The envelopes and supported payload are defined by separate method codec classes, which are similar to how message channels use message codecs. For the application of showing platform events to Flutter as a Dart stream, there is an event channel that is a specialized platform channel.


Benefits of Platform Channel

  • The most important benefit of the Platform channel is its security. In any case, no outside process can access this communication. So in terms of the level of security, Platform Channel is equivalent to any other native Android or native iOS application. ​​Communication is one of the other benefits of Platform Channel which is itself bidirectional and asynchronous so it shows that it doesn’t block execution of other tasks which are independent of native code. Last but not least is serialization and deserialization of values which you send and receive to and from messages that happen automatically.

Limitations

  • The main limitation of platform channels is that the channel method can be called only from the UI thread which is also the main isolate. Spawned Isolate can't be called by MethodChannel and EventChannel. Can't perform long-running operations on the main thread and sometimes that results in the platform side will block other message channels.


Conclusion -

The approach used by Flutter, most particularly for semantics and accessibility information, is to dynamically send updated information to the platform side whenever the information changes on the Dart side.


collect
0
avatar
Expert App Devs
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more