前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 MethodChannel 通信 )

【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 MethodChannel 通信 )

作者头像
韩曙亮
发布2023-03-29 15:53:56
9020
发布2023-03-29 15:53:56
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、MethodChannel 简介


MethodChannel 简介 : MethodChannel 通道用于方法调用 ;

一次性通信 : 该方法是一次性通信 , 在 Flutter 中调用在该方法 , 仅能调用一次 Android 方法 ;

MethodChannel 原型 :

代码语言:javascript
复制
/// 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.
///
/// See: <https://flutter.dev/platform-channels/>
class MethodChannel {
}

二、MethodChannel 在 Dart 端的实现


1、MethodChannel 构造函数

MethodChannel 的构造函数原型如下 :

代码语言:javascript
复制
class MethodChannel {
  /// Creates a [MethodChannel] with the specified [name].
  ///
  /// The [codec] used will be [StandardMethodCodec], unless otherwise
  /// specified.
  ///
  /// The [name] and [codec] arguments cannot be null. The default [ServicesBinding.defaultBinaryMessenger]
  /// instance is used if [binaryMessenger] is null.
  const MethodChannel(this.name, [this.codec = const StandardMethodCodec(), BinaryMessenger? binaryMessenger ])

  /// The logical channel on which communication happens, not null.
  final String name;

  /// The message codec used by this channel, not null.
  final MethodCodec codec;
}

MethodChannel 构造方法参数说明 :

  • String name 参数 : Channel 通道名称 , Native 应用端 与 Flutter 中的 Channel 名称 , 必须一致 ;
  • MethodCodec<T> codec 参数 : 消息编解码器 , 默认类型是 StandardMethodCodec ; Native 应用端 与 Flutter 中的消息编解码器也要保持一致 ;

2、invokeMethod 函数

创建了 MethodChannel 实例对象之后 , 通过调用

代码语言:javascript
复制
  @optionalTypeArgs
  Future<T?> invokeMethod<T>(String method, [ dynamic arguments ]) {
    return _invokeMethod<T>(method, missingOk: false, arguments: arguments);
  }

方法 , 调用 Native 端的方法 ;

invokeMethod 方法参数 / 返回值 说明 :

  • String method 参数 : Native 端的方法名 ;
  • [ dynamic arguments ] 参数 : Native 端方法传递的参数 , 这是个可变动态类型的参数 , 如果 Native 方法没有参数 , 可以选择不传递参数 ;

3、MethodChannel 使用流程

使用流程 :

首先 , 导入 Flutter 与 Native 通信 的 Dart 包 ;

代码语言:javascript
复制
import 'package:flutter/services.dart';

然后 , 定义并实现 MethodChannel 对象实例 ;

代码语言:javascript
复制
static const MethodChannel _methodChannel =
  	const MethodChannel('MethodChannel');

最后 , 调用 MethodChannel 实例对象的 invokeMethod 方法 ;

代码语言:javascript
复制
String response = await _methodChannel.invokeMethod('send', value);

三、相关资源


参考资料 :

重要的专题 :

博客源码下载 :

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、MethodChannel 简介
  • 二、MethodChannel 在 Dart 端的实现
    • 1、MethodChannel 构造函数
      • 2、invokeMethod 函数
        • 3、MethodChannel 使用流程
        • 三、相关资源
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档