Flutter Platform Channel

Platform Channel 是 Flutter 端与 Platform 端制定的通信机制,由官方提供用于 Dart 和平台之间的相互通信。
分为以下 3 种

  1. BasicMessageChannel :用于传递字符串和半结构化的信息(在大内存数据块传递的情况下使用)
  2. MethodChannel:用于传递方法调用(Method Invocation)
  3. EventChannel: 用于数据流(Event Streams)的通信

MethodChannel

iOS

private var methodChannel: FlutterMethodChannel?
methodChannel = FlutterMethodChannel(
            name: "xxxx", binaryMessenger: engine!.binaryMessenger)
// iOS --> flutter
methodChannel?.invokeMethod(method, arguments: arguments)
// flutter --> iOS
methodChannel!.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) in }

flutter

  final MethodChannel _channel = const MethodChannel('xxxx');
// native --> flutter
  _channel.setMethodCallHandler((MethodCall call) async {});
// flutter --> native
  _channel.invokeMethod("methodName").then((value) => null);
  _channel.invokeMapMethod("methodName").then((value) => null);
  _channel.invokeListMethod("methodName", []).then((value) => null);

参考:
Flutter Platform Channel深度解析
全面解析Flutter Platform Channel原理

你可能感兴趣的:(Flutter Platform Channel)