class MethodChannel
package:flutter/src/services/platform_channel.dart
A named channel for communicating with platform plugins using asynchronous method calls.
Method calls are encoded into binary before being sent, and binary results received are decoded into Dart values. The [MethodCodec] used must be compatible with the one used by the platform plugin. This can be achieved by creating a method channel counterpart of this channel on the platform side. The Dart type of arguments and results is dynamic, but only values supported by the specified [MethodCodec] can be used. The use of unsupported values should be considered programming errors, and will result in exceptions being thrown. The null value is supported for all codecs.
The logical identity of the channel is given by its name. Identically named channels will interfere with each other's communication.
一个命名通道,用于使用异步方法调用与平台插件进行通信。
方法调用在发送前被编码为二进制,接收到的二进制结果被解码为 Dart 值。 使用的 [MethodCodec] 必须与平台插件使用的兼容。 这可以通过在平台端创建该通道的方法通道副本来实现。 Dart 类型的参数和结果是动态的,但只能使用指定 [MethodCodec] 支持的值。 使用不受支持的值应被视为编程错误,并将导致抛出异常。 所有编解码器都支持空值。
通道的逻辑标识由其名称给出。 同名频道会干扰彼此的通信。
invokeMethod
调用native端方法
Future<T> invokeMethod<T>(String method, [ dynamic arguments ])
传参:
- 方法名: 和原生端一致
- 动态参数 (没有可不传)
setMethodCallHandler
接受原生页面的消息
setMethodCallHandler(Future<dynamic> handler(MethodCall call))
- call.method 方法名
- call.arguments 参数
BasicMessageChannel
BasicMessageChannel可用于flutter和原生页面传递字符串
//flutter页面监听原生页面的消息
final BasicMessageChannel _channel = BasicMessageChannel('messageChannel',StandardMessageCodec());
_channel.setMessageHandler((message){
print('${message}');
});