Introduction to communicate between Flutter and Native

Sometimes we need to communicate with native in Flutter. We need to use Platform Channels APIs for communication, mainly including the following three types:

  • MethodChanel:used for method invocation.
  • EventChannel:used for communication of event streams.
  • BasicMessageChannel:used for communication of message.

MethodChannel

Flutter calls native

First, the following functions are implemented in the flutter:

  • Create a MethodChannel and register the channel name, generally using “package name/identity” as the channel name.
  • An asynchronous call is initiated through invokeMethod.

Next, the following functions are implemented in native (android):

  • Create a MethodChannel using the same registration string as the flutter.
  • Implement and set the setMethodCallHandler.
  • Return the result to flutter through result.

Native calls flutter

The code implementation of android calling flutter is similar to that of flutter calling native (android) which via invokeMethod.

The flutter mainly implements the registration of MethodCallHandler:

EventChannel

It is used to send notification events from native to flutter. Unlike MethodChannel, EventChannel is a one-way call from native to flutter. The call is multicast (one-to-many).

Flutter part

  • Create EventChannel, use “package name/identifier” as the registration string.
  • Register the listener through StreamSubscription.listen, where the cancelOnError parameter indicates whether to automatically end listening when an error is encountered.

Native part

  • Handler registration through EventChannel.setStreamHandler.
  • EventSink sends event notification.
  • Call EventSink.endOfStream at the end of the notification, at this time onCancel will be called.
  • If necessary, you can send an error notification through EvnetSink.error, and Flutter’s StreamSubscription.onError will receive the notification.

BasicMessageChannel

It is used to send messages between flutter and native, one party sends a message to the other, and gives a reply after receiving the message.

Flutter sends message to native

First, the following functions are implemented in the flutter:

  • Create BasicMessageChannel, use “package name/identifier” as the registration string.
  • Send message via BasicMessageChannel.send

In addition to the channel name, the creation of MessageChannel also needs to specify the Encoding Method:

BasicMessageChannel(String name, MessageCodec<T> codec)

Next, the following functions are implemented in native (android):

  • Create BasicMessageChannel using the same registration string as the flutter.
  • Register MessageHandler through setHandler.
  • After receiving Message in the MessageHandler.onMessage callback, reply with reply.

Native sends message to flutter

First, the following functions are implemented in native (android):

  • Create BasicMessageChannel.
  • Send message via BasicMessageChannel.send

Next, the following functions are implemented in the flutter:

  • Create BasicMessageChannel, use “package name/identifier” as the registration string.
  • Register MessageHandler through setHandler.
  • After receiving Message in the MessageHandler, reply with reply.

Thanks for reading this and I hope this helped you develop the best app.

Senior Software Engineer