banner
jzman

jzman

Coding、思考、自觉。
github

Detailed Explanation of Platform Channel in Flutter Series

PS: In many situations, 80% of the known effects come from 20% of the possible causes.

The previous articles introduced the basics of Flutter development, including the Navigator component, Flex layout, image loading, Widget lifecycle, and hybrid development. The articles are as follows:

Next, we will introduce the use of Platform Channel in Flutter hybrid development, with the main content as follows:

  1. Introduction to Platform Channel
  2. Correspondence of Platform Data Types
  3. BasicMessageChannel
  4. MethodChannel
  5. EventChannel

Introduction to Platform Channel#

Platform Channel is an asynchronous message channel where messages are encoded into binary messages before being sent, and the received binary messages are decoded into Dart values. The types of messages that can be passed are limited to those supported by the corresponding decoders, and all decoders support empty messages. The communication architecture between Native and Flutter is shown in the figure below:

image

Three different types of PlatformChannel are defined in Flutter, mainly as follows:

  • BasicMessageChannel: Used for data transmission;
  • MethodChannel: Used for method calls;
  • EventChannel: Used for event transmission;

All constructors require specifying a channel identifier, decoder, and BinaryMessenger. BinaryMessenger is a communication tool between Flutter and the platform, used to transmit binary data and set corresponding message handlers.

There are two types of decoders: MethodCodec and MessageCodec, where the former corresponds to methods and the latter corresponds to messages. BasicMessageChannel uses MessageCodec, while MethodChannel and EventChannel use MethodCodec.

Correspondence of Platform Data Types#

Platform Channel provides different message decoding mechanisms, such as StandardMessageCodec for basic data type decoding and JSONMessageCodec for JSON decoding. Automatic conversion occurs during communication between platforms, and the correspondence of data types across platforms is as follows:

image

BasicMessageChannel#

BasicMessageChannel is mainly used for data transmission, including binary data. With BasicMessageChannel, the functionalities of MethodChannel and EventChannel can be achieved. Here, we use BasicMessageChannel to implement a case where an Android project uses Flutter resource files, with the key process as follows:

  1. The Flutter side obtains the binary data corresponding to the image resource, using BinaryCodec, resulting in data formatted as ByteData;
  2. Use BasicMessageChannel to send the data corresponding to the image;
  3. On the Android side, use ByteBuffer to receive it, convert it to ByteArray, and then parse it into a Bitmap for display.

The key code on the Flutter side is as follows:

The key code on the Android side is as follows:

Additionally, BasicMessageChannel combined with BinaryCodec supports the transmission of large memory data blocks.

MethodChannel#

MethodChannel is mainly used for method transmission, allowing the passing of both Native methods and Dart methods. This means that MethodChannel can be used to call Android native methods from Flutter and Dart methods from Android, with mutual calls made through the invokeMethod method of MethodChannel. Communication must use the same channel identifier, as detailed below:

  1. Flutter calls Android methods

Below is the implementation of jumping from Flutter to the Android native interface MainActivity using MethodChannel on the Android side:

As shown above, the MethodChannel.Result object can also be used to callback execution results to Flutter. The Flutter side is as follows:

  1. Android calls Dart methods

Below is the implementation of calling the Dart method getName in Flutter using MethodChannel on the Android side:

The Flutter side is as follows:

EventChannel#

EventChannel is mainly used for one-way calls from Flutter to Native, and its usage is similar to broadcasting in Android. The native interface is responsible for sending events, while the Flutter side registers to listen. Without further ado, let's look at the code. The Android side code is as follows:

The Flutter side is as follows:

This concludes the use of Flutter Platform Channels.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.