24

In Flutter, there are three types of platform channels, and I want to know about the difference between them.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
james deng
  • 243
  • 1
  • 2
  • 4

3 Answers3

38

These channels are used to communicate between native code (plugins or native code inside of your project) and the Flutter framework.

MethodChannel

A MethodChannel is used for "communicating with platform plugins using asynchronous method calls". This means that you use this channel to invoke methods on the native side and can return back a value and vise versa.
You can e.g. call a method that retrieves the device name this way.

EventChannel

An EventChannel is used to stream data. This results in having a Stream on the Dart side of things and being able to feed that stream from the native side.
This is useful if you want to send data every time a particular event occurs, e.g. when the wifi connection of a device changes.

BasicMessageChannel

This is probably not something you will want to use. BasicMessageChannel is used to encode and decode messages using a specified codec.
An example of this would be working with JSON or binary data. It is just a simpler version because your data has a clear type (codec) and you will not send multiple parameters etc.

Community
  • 1
  • 1
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
2

Here is a link to a good explanation for you https://medium.com/flutter-io/flutter-platform-channels-ce7f540a104e

Basically there are two main types:

Method Channels: designed for invoking named pieces of code across Dart and Java/Kotlin or Objective-C/Swift. (From flutter to the platform)

Event Channels: specialized platform channel intended for the use case of exposing platform events to Flutter as a Dart stream. (From the platform to flutter)

Bryan
  • 71
  • 4
0

@creativecreatorormaybenot answer clears the things, let me add more to this.

Method Channel

This is more like RPC call. You invoke a method from your Flutter app to the native code, the native code does something and finally responds with a success or error. This call could be to get the current battery status, network information or temperature data. Once the native side has responded, it can no longer send more information until the next call.

Method Channel provides platform communication using asynchronous method calls.

Note:- If desired, method calls can also be sent in the reverse direction, with the platform acting as client to methods implemented in Dart.

Event Channel

This is more like reactive programming where platform communication using asynchronous event streams. These events could be anything that you need streamed to your Flutter application.Streaming data from the native code to Flutter app like continuously updating BLE or WiFi scanning results, accelerometer and gyro, or even periodic status updates from intensive data collection.

Basic Message Channel

It provides basic messaging services similar to BinaryMessages, but with pluggable message codecs in support of sending strings or semi-structured messages. Messages are encoded into binary before being sent, and binary messages received are decoded into Dart values. The MessageCodec used must be compatible with the one used by the platform plugin.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49