日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/6/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 BasicMessageChannel 通信 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、BasicMessageChannel 簡介
  • 二、BasicMessageChannel 在 Dart 端的實現(xiàn)
    • 1、BasicMessageChannel 構造方法
    • 2、使用 BasicMessageChannel 接收 Native 發(fā)送的消息
    • 3、使用 BasicMessageChannel 向 Native 發(fā)送消息
    • 4、BasicMessageChannel 使用流程
  • 三、相關資源





一、BasicMessageChannel 簡介



BasicMessageChannel 簡介 :

這是一個命名通道 , 用于 Flutter 端 與 Native 端的消息傳遞 ;

發(fā)送消息前 , 先編碼成二進制信息 , 接收后再將二進制信息解碼成對應類型的數(shù)據(jù) ;

如上圖所示 , 如果從 Flutter 端向 Android 端發(fā)送 int 類型數(shù)據(jù) , 將 Dart 中的 int 類型 轉為 Android 端的 Integer 類型 ;

只支持上圖中的類型 , 即基本數(shù)據(jù)類型和集合類型 , 不支持自定義類型 ;


BasicMessageChannel 原型 :

/// A named channel for communicating with platform plugins using asynchronous /// message passing. /// /// Messages are encoded into binary before being sent, and binary messages /// received are decoded into Dart values. The [MessageCodec] used must be /// compatible with the one used by the platform plugin. This can be achieved /// by creating a basic message channel counterpart of this channel on the /// platform side. The Dart type of messages sent and received is [T], /// but only the values supported by the specified [MessageCodec] can be used. /// The use of unsupported values should be considered programming errors, and /// will result in exceptions being thrown. The null message 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 BasicMessageChannel<T> { }

可回復 : 使用該 BasicMessageChannel 通道發(fā)送數(shù)據(jù) , 對方收到消息后 , 可以進行回復 ;

持續(xù)發(fā)送 : BasicMessageChannel 通道可以持續(xù)發(fā)送數(shù)據(jù) ;


常用場景 :

  • 持續(xù)遍歷 : 在 Android 端遍歷數(shù)據(jù) , 將遍歷信息持續(xù)發(fā)送給 Flutter 端 ;
  • 耗時操作 : Flutter 需要處理耗時計算 , 將信息傳給 Android , Android 處理完后 , 回傳給 Flutter 計算結果 ;




二、BasicMessageChannel 在 Dart 端的實現(xiàn)




1、BasicMessageChannel 構造方法


Dart 端 BasicMessageChannel 構造函數(shù)原型如下 :

/// Creates a [BasicMessageChannel] with the specified [name], [codec] and [binaryMessenger].////// The [name] and [codec] arguments cannot be null. The default [ServicesBinding.defaultBinaryMessenger]/// instance is used if [binaryMessenger] is null.const BasicMessageChannel(this.name, this.codec, { BinaryMessenger? binaryMessenger })/// The logical channel on which communication happens, not null.final String name;/// The message codec used by this channel, not null.final MessageCodec<T> codec;

下面介紹構造函數(shù)的參數(shù) :

String name 參數(shù) : Channel 通道名稱 , Native 應用端 與 Flutter 中的 Channel 名稱 , 必須一致 ;

MessageCodec<T> codec 參數(shù) : 消息編解碼器 , 有 444 中實現(xiàn)類型 ; Native 應用端 與 Flutter 中的消息編解碼器也要保持一致 ;


2、使用 BasicMessageChannel 接收 Native 發(fā)送的消息


創(chuàng)建好 BasicMessageChannel 消息通道后 , 需要為該 Channel 通道設置一個 MessageHandler 消息處理器 , 調用 BasicMessageChannel 的 setMessageHandler 方法 , 設置該消息處理器 ;

這樣在 Flutter 的 Dart 端才能接收到 Android Native 端傳遞來的消息 ;


BasicMessageChannel 的 setMessageHandler 方法原型 :

/// Sets a callback for receiving messages from the platform plugins on this/// channel. Messages may be null.////// The given callback will replace the currently registered callback for this/// channel, if any. To remove the handler, pass null as the `handler`/// argument.////// The handler's return value is sent back to the platform plugins as a/// message reply. It may be null.void setMessageHandler(Future<T> Function(T? message)? handler) {if (handler == null) {binaryMessenger.setMessageHandler(name, null);} else {binaryMessenger.setMessageHandler(name, (ByteData? message) async {return codec.encodeMessage(await handler(codec.decodeMessage(message)));});}}

傳入的參數(shù)是 Future<T> handler(T message) , 該參數(shù)是用于消息處理的 , 需要配合 BinaryMessenger 進行消息處理 ;


3、使用 BasicMessageChannel 向 Native 發(fā)送消息


在 Flutter 端如果想 Native 端發(fā)送消息 , 使用 BasicMessageChannel 的 send 方法即可 ;

send 方法原型 :

/// Sends the specified [message] to the platform plugins on this channel.////// Returns a [Future] which completes to the received response, which may/// be null.Future<T?> send(T message) async {return codec.decodeMessage(await binaryMessenger.send(name, codec.encodeMessage(message)));}

send 方法 參數(shù) / 返回值 分析 :

  • T message 參數(shù) : Flutter 端要發(fā)送給 Native 端的消息 ;
  • Future<T> 返回值 : Native 端回送給 Flutter 端的消息 ;

該 send 方法接收一個 Future<T> 類型返回值 , 該返回值是異步的 ;

也就是說 Dart 端向 Native 端發(fā)送一個消息 , Native 端處理完畢后 , 會回傳一個異步消息 ;


4、BasicMessageChannel 使用流程


BasicMessageChannel 使用流程 :

首先 , 導入 Flutter 與 Native 通信 的 Dart 包 ;

import 'package:flutter/services.dart';

然后 , 定義并實現(xiàn) MethodChannel 對象實例 ;

static const BasicMessageChannel _basicMessageChannel =const BasicMessageChannel('BasicMessageChannel', StringCodec());

最后 , 從 BasicMessageChannel 消息通道接收信息 ;

/// 接收 Native 消息 , 并進行回復 /// 從 BasicMessageChannel 通道獲取消息 _basicMessageChannel.setMessageHandler((message) => Future<String>((){setState(() {showMessage = "BasicMessageChannel : $message";});return "BasicMessageChannel : $message"; }));

或者 , 通過 BasicMessageChannel 向 Native 發(fā)送消息 ;

/// 向 Native 發(fā)送消息try {String response = await _basicMessageChannel.send(value);} on PlatformException catch (e) {print(e);}



三、相關資源



參考資料 :

  • Flutter 官網(wǎng) : https://flutter.dev/
  • Flutter 插件下載地址 : https://pub.dev/packages
  • Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區(qū) : https://flutter.cn/
  • Flutter 實用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文檔 : https://dart.cn/
  • Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
  • Flutter 中文網(wǎng) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相關問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )
  • GitHub 上的 Flutter 開源示例 : https://download.csdn.net/download/han1202012/15989510
  • Flutter 實戰(zhàn)電子書 : https://book.flutterchina.club/chapter1/
  • Dart 語言練習網(wǎng)站 : 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 端实现 BasicMessageChannel 通信 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 色哟哟视频| 国产精品热久久 | 伊人网站在线观看 | 性网| 亚洲另类欧美日韩 | 日日爱669 | a级片黄色| 4438x五月天 日吊视频 | 九九九久久久精品 | 蜜臀av在线观看 | 好吊操视频这里只有精品 | 人与动物2免费观看完整版电影高清 | 超碰人人草 | 草草草在线观看 | 久久精品美女视频 | 青草视屏| 91不卡在线 | 成人自拍在线 | 国产欧美久久久久 | 91麻豆精品国产91久久久久久久久 | 精品不卡一区二区三区 | 国产精品成人av久久 | av色婷婷| 日本不卡视频一区 | 免费视频黄色 | 国产精品久久久久久在线观看 | 色天堂影院 | 国产婷婷一区二区 | 成人免费va视频 | 欧美成人精品三级网站 | 欧美特黄 | 好吊视频在线观看 | 麻豆自拍视频 | 国产成人无码精品 | 黄瓜视频在线免费观看 | 女人十八岁毛片 | 亚洲精品综合在线 | 日韩成人影视 | 91在线看视频 | 亚洲高清在线一区 | 蜜桃免费在线视频 | 黄色小视频免费看 | 91丨porny丨 | 免费黄色在线视频 | 亚洲xxxx18| 亚洲一级无毛 | 久久国产综合 | 91官网入口 | 黄色特一级 | 国产影视一区二区 | 韩国美女av | 尤物在线视频 | 黄色大片日本 | 亚洲欧美一区二区三区在线 | 国产麻豆电影在线观看 | 天堂av亚洲| 亚洲无毛视频 | 国产午夜精品福利视频 | 麻豆偷拍 | 亚洲国产精品无码久久久久高潮 | 拔插拔插海外华人永久免费 | 福利视频在线看 | 亚洲欧美日韩另类 | 鸭子av| 一区二区亚洲精品 | 日韩经典在线 | 日本泡妞xxxx免费视频软件 | 国产精品久线在线观看 | 日韩五码 | 久热这里只有 | 白丝久久 | 999视频在线播放 | 国产一区二区三区免费播放 | 天天噜日日噜 | 国产乱码精品1区2区3区 | 中文字幕在线播放 | 青草伊人久久 | 亚洲精品二区三区 | 最新理伦片eeuss影院 | 成人毛片100部免费看 | 日本免费三片在线播放 | 欧美123区| 国产日韩欧美中文字幕 | 射久久久 | 漂亮人妻被中出中文字幕 | 国产精品五区 | 波多野吉衣在线观看视频 | 看一级黄色 | 99久在线精品99re8热 | 污污视频网站在线免费观看 | 久久久www成人免费无遮挡大片 | 香蕉视频成人在线观看 | www婷婷| 超碰人人搞 | 日本一级淫片 | 美利坚合众国av | 免费黄色av网址 | 国产精品1区2区3区 在线看黄的网站 | 在线观看你懂的网站 |