channel通信linux,Flutter之 windows desktop method channel 本地通信

cc0ce5784922

flutter2.png

Flutter2.0

Flutter2.0已经发布了,带来了很多新消息。使开发人员能够为任何平台创建美观、快速且可移植的应用程序。 借助 Flutter 2,可以使用相同的代码库将本机应用程序发布到五个操作系统: iOS,Android,Windows,macOS 和 Linux; 以及针对 Chrome,Firefox,Safari 或 Edge 等浏览器的 Web 体验。 Flutter 甚至可以嵌入到汽车,电视和智能家电中,为环境计算世界提供普遍且可延展的体验。

现在终于可以在stable下直接开发桌面应用了。最近刚好在做一个答题卡识别APP,需要做Windows版本(内心是不想的,无奈Windows系统用户多)。正好体验一下更新之后的变化。

flutter windows desktop

首先,切换到stable channel:

flutter channel stable

更新

flutter upgrade

打开Windows、macOS、Linux桌面支持

flutter config --enable-windows-desktop

flutter config --enable-macos-desktop

flutter config --enable-linux-desktop

创建项目

flutter create xxx

已有项目使用

flutter create .

即可重新生成项目。

method channel

使用平台通道在flutter和native本地程序之间进行传递消息。Method channel就是平台通道的api。

cc0ce5784922

PlatformChannels.png

网上和官方能查到的都是关于iOS、Android平台的使用教程,关于flutter windows桌面平台的资料几乎没有,在github上有看到一些issue。内容也不够准确,通过自己的尝试,最后终于找到了方法。

在flutter windows项目目录,找到runner文件夹下的flutter_windows_.cpp,添加如下代码:

#include "flutter/method_channel.h"

#include "flutter/standard_method_codec.h"

void configMethodChannel(flutter::FlutterEngine *engine) {

const std::string test_channel("com.caidan.yuejuan/scan");

const flutter::StandardMethodCodec& codec = flutter::StandardMethodCodec::GetInstance();

flutter::MethodChannel method_channel_(engine->messenger(), test_channel, &codec);

method_channel_.SetMethodCallHandler([](const auto& call, auto result) {

std::cout << "Inside method call" << std::endl;

if (call.method_name().compare("__closeWindow__") == 0) {

std::cout << "Close window message recieved!" << std::endl;

result->Success();

}

else if (call.method_name().compare("goToNativeScanPage") == 0) {

std::cout << "goToNativeScanPage!" << std::endl;

result->Success();

}

});

}

在OnCreate方法中,调用configMethodChannel方法即可。

在dart代码中添加调起的方法就不赘述,不知道得可以看官方教程https://flutterchina.club/platform-channels/

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值