一 概述
Flutter与Native原生端通信有三种方法:
- MethodChannel
- BasicMessageChannel
- EventChannel
二 三种通信方式介绍
2.1 MethodChannel
- Flutter与Native端相互调用,调用后返回结果
- 可以Native端主动调用,也可以Flutter主动调用,属于双向通信
- 此种方式最为常见,Native端调用需要在主线程中执行
2.2 BasicMessageChannel
- 用于使用指定的编解码器对消息进行编码和解码
- 属于双向通信,可以以Native端主动调用,也可以Flutter主动调用
2.3 EventChannel
- 用于数据流(event stream)的通信,Native端主动发送数据给Flutter
- 通常用于状态端监听,比如网络变化、传感器数据等
三 通信架构图
此图为官方的架构图

本文介绍了Flutter与Native原生端的三种通信方式:MethodChannel、BasicMessageChannel和EventChannel。MethodChannel是双向通信,常用于调用后返回结果的操作;BasicMessageChannel侧重于自定义编解码的消息传递;EventChannel则用于Native端向Flutter发送数据流,适用于状态监听。了解这些通信方式对于Flutter与Native的集成至关重要。
3735

被折叠的 条评论
为什么被折叠?



