Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between MethodChannel, EventChannel & BasicMessageChannel?

Tags:

flutter

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

like image 642
james deng Avatar asked May 16 '19 14:05

james deng


People also ask

What is Methodchannel?

A named channel for communicating with the Flutter application using asynchronous method calls. Incoming method calls are decoded from binary on receipt, and Java results are encoded into binary before being transmitted back to Flutter.

What is EventChannel in Flutter?

An Event Channel is an object that allows you to constantly send information from the native portion of your app to the Flutter portion of your app via a stream.

What is Event Channel How is it different from the platform channel?

EventChannel exposes data from the platform to Dart as streams. When you subscribe to an event channel on the Dart end, you get a continuous stream of data from iOS or Android.


Video Answer


2 Answers

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.

like image 98
creativecreatorormaybenot Avatar answered Oct 13 '22 01:10

creativecreatorormaybenot


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)

like image 45
Bryan Avatar answered Oct 13 '22 01:10

Bryan