Flutter MethodChannel 学习

Flutter MethodChannel 学习

好久没写东西,正好最近有时间又开始研究起来Flutter,不了解的童鞋可以去查查资料了解一下, 在我看来是目前我了解盗的最理想的跨平台解决方案了。而跨平台中一个比较重要的点就是与底层进行交互,本文就主要说一下在Flutter中是如何与Android进行通讯的(iOS应该同理)。不涉及到源码,只是简单的使用和在使用过程中的一些想法。

创建初始的Demo

使用 flutter create methodchannellearn 创建一个 flutter 项目。

导入 servicesasync
import 'dart:async'; import 'package:flutter/services.dart';

首先先在main.dart_MyHomePageState 中创建一个静态成员变量methodChannel
static const MethodChannel methodChannel=const MethodChannel("flutter_method_channel");

然后修改原有的_incrementCounter()方法,内部调用methodChannel.invokeMethod()方法,调用Native端的方法。

void _incrementCounter() { // setState(() { // // This call to setState tells the Flutter framework that something has // // changed in this State, which causes it to rerun the build method below // // so that the display can reflect the updated values. If we changed // // _counter without calling setState(), then the build method would not be // // called again, and so nothing would appear to happen. // _counter++; // }); methodChannel.invokeMethod("_incrementCounter", _counter++); }

接下来就是在Android端添加响应的代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this); new MethodChannel(getFlutterView(), "flutter_method_channel").setMethodCallHandler( new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { if (methodCall.method.equals("_incrementCounter")) { toast(methodCall.arguments); } } }); }

跑一下之后可以看到正常的toast弹出。

添加回调

onMethodCall中我们可以看到还有另外的一个参数result,可以使用它将数据传递回Flutter中。

修改Android端代码

if (methodCall.method.equals("_incrementCounter")) { if(methodCall.arguments instanceof Integer){ int count= (int) methodCall.arguments; count++; result.success(count); } }

同时修改Flutter中的代码

Future<Null> _incrementCounter() async { // setState(() { // // This call to setState tells the Flutter framework that something has // // changed in this State, which causes it to rerun the build method below // // so that the display can reflect the updated values. If we changed // // _counter without calling setState(), then the build method would not be // // called again, and so nothing would appear to happen. // _counter++; // }); var result = await methodChannel.invokeMethod("_incrementCounter", _counter); setState(() { _counter = result; }); }

运行一下,点击次数正常增加。

主动通知Flutter

有时候我们可能会需要主动的通知flutter ,而不是等待flutter调用,利用回调传递数据,这是就需要一个新的Channel, EventChannel

同样还是刚才的例子,这会先修改flutter代码,使用EventChannel

static const EventChannel eventChannel = const EventChannel("flutter_event_channel"); void _onData(event) { setState(() { _counter = event; }); } @override void initState() { // TODO: implement initState super.initState(); eventChannel.receiveBroadcastStream().listen(_onData); }

然后修改Android的代码

new MethodChannel(getFlutterView(), "flutter_method_channel").setMethodCallHandler( new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { if (methodCall.method.equals("_incrementCounter")) { if (methodCall.arguments instanceof Integer) { int count = (int) methodCall.arguments; count++; if (eventSink != null) { eventSink.success(count); } } } } }); new EventChannel(getFlutterView(), "flutter_event_channel").setStreamHandler( new EventChannel.StreamHandler() { @Override public void onListen(Object o, EventChannel.EventSink eventSink) { MainActivity.this.eventSink = eventSink; } @Override public void onCancel(Object o) { MainActivity.this.eventSink = null; } });

同样 运行,数据也能够正常显示

这里EventChannel的使用场景应该会有好多,毕竟一个持久长时间的消息通道在实际场景中会有很多应用。

项目的代码地址在这里

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • 有很长一段时间,电影和各种演出淡出了我的世界,在居住的小镇,电影院已经关闭。直到孩子大了一点,直到我们有了车,我们...
    薇薇安的30天阅读 766评论 0 0
  • 20年前,我参加了【战神之道】,至今回忆,依然如当年的动情! 这场战争,预言了我的人生!记得在硝烟弥漫的空旷里...
    安清King阅读 644评论 0 0
  • 2018.3.17 星期六 天气:雨 事出有因(原创) 今天去市一中参加中招备考会,遇到了在兄弟...
    你健康我快乐_61fc阅读 198评论 0 3