文章目录

  • 一、MethodChannel 简介
  • 二、MethodChannel 在 Dart 端的实现
    • 1、MethodChannel 构造函数
    • 2、invokeMethod 函数
    • 3、MethodChannel 使用流程
  • 三、相关资源

一、MethodChannel 简介


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

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

MethodChannel 原型 :

/// 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 的构造函数原型如下 :

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 实例对象之后 , 通过调用

  @optionalTypeArgsFuture<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 包 ;

import 'package:flutter/services.dart';

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

static const MethodChannel _methodChannel =const MethodChannel('MethodChannel');

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

String response = await _methodChannel.invokeMethod('send', value);

三、相关资源


参考资料 :

  • Flutter 官网 : https://flutter.dev/
  • Flutter 插件下载地址 : https://pub.dev/packages
  • Flutter 开发文档 : https://flutter.cn/docs ( 强烈推荐 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社区 : https://flutter.cn/
  • Flutter 实用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文档 : https://dart.cn/
  • Dart 开发者官网 : https://api.dart.dev/
  • Flutter 中文网 : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )
  • GitHub 上的 Flutter 开源示例 : https://download.csdn.net/download/han1202012/15989510
  • Flutter 实战电子书 : https://book.flutterchina.club/chapter1/
  • Dart 语言练习网站 : https://dartpad.dartlang.org/

重要的专题 :

  • Flutter 动画参考文档 : https://flutterchina.club/animations/

博客源码下载 :

  • GitHub 地址 : ( 随博客进度一直更新 , 有可能没有本博客的源码 )

    • Flutter Module 工程 : https://github.com/han1202012/flutter_module
    • Android 应用 : https://github.com/han1202012/flutter_native
    • 注意 : 上面两个工程要放在同一个目录中 , 否则编译不通过 ;
  • 博客源码快照 : https://download.csdn.net/download/han1202012/21670919 ( 本篇博客的源码快照 , 可以找到本博客的源码 )

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

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

    文章目录 前言 一.Android 端 MethodChannel 构造函数 二.Android 端 setMethodCallHandler 方法 三.Android 端实现 MethodChann ...

  2. 干货 | 携程APP Native/RN内嵌Flutter UI混合开发实践和探索

    作者简介 Deway,携程资深工程师,iOS客户端开发,热衷于大前端和动态化技术: Frank,携程高级工程师,关注移动端热门技术,安卓客户端开发. 前言 随着各种多端技术的蓬勃发展,如今的移动端和前 ...

  3. 如何用 Flutter 实现混合开发?闲鱼公开源代码实例

    2019独角兽企业重金招聘Python工程师标准>>> 具有一定规模的 App 通常有一套成熟通用的基础库,尤其是阿里系 App,一般需要依赖很多体系内的基础库.那么使用 Flutt ...

  4. Flutter混合开发:Android中如何启动Flutter

    目录 现有项目中引入Flutter 启动flutter页面 加速启动 启动传参 flutter可以独立完成项目,但是在现有项目情况下最好的方式就是混合开发,逐步过渡.这样就会共存native和flut ...

  5. flutter开发android部分页面,Flutter(Android 混合开发)

    前言 Flutter 支持作为 android Moudle 出现在项目中.这样就可以在 已有的项目中 使用. 虽然现在Flutter 比较受关注,但是和weex 一样 ,大部分都只是在观望 不是真正 ...

  6. Flutter 深入探索混合开发的技术演进

    关于 Flutter 混合 PlatformView 的实现已经介绍过两次,随着 5 月份谷歌 IO 的接近,新的 PlatformView 实现应该也会随之而来,本次就从头到尾来一个详细的关于 Pl ...

  7. 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 完整代码示例 )

    文章目录 前言 一.Android 端完整代码示例 二.Flutter 端完整代码示例 三.相关资源 前言 前置博客 : [Flutter]Flutter 混合开发 ( Flutter 与 Nativ ...

  8. 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 EventChannel 通信 )

    文章目录 前言 一.Android 端 EventChannel 构造函数 二.Android 端 setStreamHandler 方法 三.Android 端实现 EventChannel 通信步 ...

  9. 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | Android 端实现 BasicMessageChannel 通信 )

    文章目录 前言 一.Android 端 BasicMessageChannel 构造函数 二.Android 端 MessageCodec 子类实现 三.Android 端 setMessageHan ...

最新文章

  1. 测试晶面间距软件_纳米材料粒度测试方法大全
  2. appnode php,环境软件路径参考
  3. 搭建Hadoop的HA高可用架构(超详细步骤+已验证)
  4. Linux下日志分析的几个常用命令
  5. Word表格高度不能调小
  6. 计算机密码发明者去世!曾获图灵奖、并启蒙 Unix 诞生!
  7. zabbix3.0 监控mysql服务器性能实现过程
  8. Atlassian JIRA 插件开发之一 环境搭建
  9. 阿里云ECS云服务器快照
  10. thuwc2019滚粗记
  11. Python 哥德巴赫猜想
  12. 数据库中COMMENT关键字的使用
  13. blog推荐 - 左岸读书
  14. python pdf 加水印_Python中通过PyPDF2实现PDF添加水印
  15. 单点登录 Ucenter 分析
  16. 分享数百个 HT 工业互联网 2D 3D 可视化应用案例
  17. 【软件测试】——编写测试用例实例
  18. 【前端期末作业 基于jQuery鲜花销售管理系统】
  19. SWF to EXE 工具制作
  20. Adobe Photoshop 2020 21.2.2.289 中文版 — 图像处理工具

热门文章

  1. 无法嵌入互操作类型 请改用适用的接口。
  2. FLASHBACK实施笔记
  3. 给程序员的VIM速查卡
  4. MySQL InnoDB引擎锁的总结
  5. 在windows平台使用Apache James搭建邮件服务器以及使用C#向外网发送邮件
  6. Java实现单例模式
  7. 最长上升子序列(LIS)
  8. [Apple开发者帐户帮助]三、创建证书(1)证书概述
  9. 第六周实践作业:软件测试和评估
  10. 初始html(常用标签)