官方推荐插件辅助工具 - pigeon
参考文章:Flutter学习-Channel详解
一、通道channel分类
1、BasicMessageChannel
用于支持MessageCodec
编解码器的类型消息的异步传递通道。
2、MethodChannel
用于支持MethodCodec
编解码器的方法的异步调用通道。
3、OptionalMethodChannel
在MethodChannel
基础上,支持了当invokeMethod
没有找到平台插件时,不会抛出异常而返回null
。
4、EventChannel
用于事件流的传递,但是事件值必须支持MethodCodec
编解码器。

二、通道channel
结构
1、结构
每个channel
都有3个参数:name
、MessageCodec
/MethodCodec
、BinaryMessenger
name
:用通道名称来标识通道的唯一。
MessageCodec
/MethodCodec
:通道数据的编解码器。
BinaryMessenger
:二进制消息数据的发送和接收信使。
2、BinaryMessenger
信使
Flutter中定义为抽象类abstract class BinaryMessenger
,其中定义了几个接口:
Future<void> handlePlatformMessage(String channel, ByteData? data, ui.PlatformMessageResponseCallback? callback);
Future<ByteData?> send(String channel, ByteData? message);
void setMessageHandler(String channel, MessageHandler? handler);
bool checkMessageHandler(String channel, MessageHandler? handler);
void setMockMessageHandler(String channel, MessageHandler? handler);
bool checkMockMessageHandler(String channel, MessageHandler? handler);
iOS
中则对应为:
@protocol FlutterBinaryMessenger <NSObject>
/**
* Sends a binary message to the Flutter side on the specified channel, expecting
* no reply.
*
* @param channel The channel name.
* @param message The message.
*/
- (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message;
/**
* Sends a binary message to the Flutter side on the specified channel, expecting
* an asynchronous reply.
*
* @param channel The channel name.
* @param message The message.
* @param callback A callback for receiving a reply.
*/
- (void)sendOnChannel:(NSString*)channel
message:(NSData* _Nullable)message
binaryReply:(FlutterBinaryReply _Nullable)callback;
/**
* Registers a message handler for incoming binary messages from the Flutter side
* on the specified channel.
*
* Replaces any existing handler. Use a `nil` handler for unregistering the
* existing handler.
*
* @param channel The channel name.
* @param handler The message handler.
* @return An identifier that represents the connection that was just created to the channel.
*/
- (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel
binaryMessageHandler:
(FlutterBinaryMessageHandler _Nullable)handler;
/**
* Clears out a channel's message handler if that handler is still the one that
* was created as a result of
* `setMessageHandlerOnChannel:binaryMessageHandler:`.
*
* @param connection The result from `setMessageHandlerOnChannel:binaryMessageHandler:`.
*/
- (void)cleanupConnection:(FlutterBinaryMessengerConnection)connection;
@end
在iOS
端,binaryMessenger
为FlutterViewController
的一个属性,通过继承来获取binaryMessenger
。
具体在创建channel的时候使用:
let aliPayMethodChannel = FlutterMethodChannel(name: "aliPay", binaryMessenger: binaryMessenger)
三、数据类型对应
Flutter
、Android
、iOS
各个平台数据类型对应关系如下:

在进行数据传递的时候,通过转为字节byte
进行转换。
四、消息的编码解码器
1、 MessageCodec
包括:BinaryCodec
、StringCodec
、JSONMessageCodec
、StandardMessageCodec
// 直接使用二进制数据
class BinaryCodec implements MessageCodec<ByteData?>
// 字符串与二进制数据之间的转换
class StringCodec implements MessageCodec<String?>
// utf8编码的json数据与二进制数据的转换 - 用于大部分场景
class JSONMessageCodec implements MessageCodec<dynamic>
class StandardMessageCodec implements MessageCodec<dynamic>
2、 MethodCodec
包括:JSONMethodCodec
、StandardMethodCodec
class JSONMethodCodec implements MethodCodec
class StandardMethodCodec implements MethodCodec
五、给channel
设置回调Handler
每一种channel
对应一种回调,包含有以下3种:
typedef void (^FlutterMessageHandler)(id _Nullable message, FlutterReply callback);
typedef void (^FlutterMethodCallHandler)(FlutterMethodCall* call, FlutterResult result);
@protocol FlutterStreamHandler
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
eventSink:(FlutterEventSink)events;
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments;
@end
六、线程
Flutter
中的线程
共有4个线程:
1、Platform thread
(平台线程):plugin
的代码运行在这里,对应安卓和iOS
的main
主线程
2、UI thread
(UI
线程):执行Dart
代码的线程,包括项目代码和Flutter
框架的代码。UI
线程会生成一个图层树,发送到GPU
线程渲染到设备。
3、Raster thread
(光栅线程):拿到图层树后,交给GPU
渲染处理。
4、I/O
线程:执行I/O
输入和输出操作。
由此可知,channel
在原生侧运行在Platform thread
(平台线程)里,即原生的主线程里。
网友评论