文章目录

  • 一、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. 什么是Python?主要运用哪些方面?
  2. ubuntu权限管理
  3. C++ OOP学习记录
  4. 金融贷款逾期的模型构建1
  5. 做一个有梦想的咸鱼!
  6. 轻量级的Ajax解决方案——DynAjax:直接在客户端调用C#类的方法
  7. [css] 要让Chrome支持小于12px的文字怎么做?
  8. 20200724:力扣197周周赛上
  9. 连接mysql数据库格式_MySQL_Mysql数据库命令大全,一、连接Mysql格式: mysql -h - phpStudy...
  10. 超大文件中查找关键字
  11. USB协议详解第17讲(USB事务总结)
  12. windows无法格式化u盘_U盘打不开?无法格式化?试试这个方法可能有效!
  13. STM32-StdPeriph函数库使用的预备知识
  14. android自定义系统屏保Dream
  15. FileNotFoundError: [Errno 2] No such file or directory: ‘./train/hazy/hazy‘
  16. AppleID加入苹果开发团队
  17. 在线直播授课系统哪个好?哪个更适合做网校?
  18. 国内外IoT系统介绍
  19. 我确实不知道如何使用计算机翻译,新视野大学英语4课后翻译
  20. 信息学奥赛一本通1258:【例9.2】数字金字塔题解

热门文章

  1. 深入解析Javascript中this关键字的使用
  2. Windows Server 2008标准证书使用记录
  3. Intellij IDEA常用配置详解
  4. [POJ 2329] Nearest number-2
  5. linux配置ip 网关 和dns(转)
  6. 07. 分页写法小结
  7. [erlang]proc_lib源码浅析
  8. outlookbar control
  9. Excel的日期格式约定与解析
  10. 文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: FormMain --- 未能加载基类...