What is the difference between MethodChannel, EventChannel & BasicMessageChannel?

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)


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.

Tags:

Flutter