【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 BasicMessageChannel 通信 )
文章目錄
- 一、BasicMessageChannel 簡介
- 二、BasicMessageChannel 在 Dart 端的實現
- 1、BasicMessageChannel 構造方法
- 2、使用 BasicMessageChannel 接收 Native 發送的消息
- 3、使用 BasicMessageChannel 向 Native 發送消息
- 4、BasicMessageChannel 使用流程
- 三、相關資源
一、BasicMessageChannel 簡介
BasicMessageChannel 簡介 :
這是一個命名通道 , 用于 Flutter 端 與 Native 端的消息傳遞 ;
發送消息前 , 先編碼成二進制信息 , 接收后再將二進制信息解碼成對應類型的數據 ;
如上圖所示 , 如果從 Flutter 端向 Android 端發送 int 類型數據 , 將 Dart 中的 int 類型 轉為 Android 端的 Integer 類型 ;
只支持上圖中的類型 , 即基本數據類型和集合類型 , 不支持自定義類型 ;
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 通道發送數據 , 對方收到消息后 , 可以進行回復 ;
持續發送 : BasicMessageChannel 通道可以持續發送數據 ;
常用場景 :
- 持續遍歷 : 在 Android 端遍歷數據 , 將遍歷信息持續發送給 Flutter 端 ;
- 耗時操作 : Flutter 需要處理耗時計算 , 將信息傳給 Android , Android 處理完后 , 回傳給 Flutter 計算結果 ;
二、BasicMessageChannel 在 Dart 端的實現
1、BasicMessageChannel 構造方法
Dart 端 BasicMessageChannel 構造函數原型如下 :
/// 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;下面介紹構造函數的參數 :
String name 參數 : Channel 通道名稱 , Native 應用端 與 Flutter 中的 Channel 名稱 , 必須一致 ;
MessageCodec<T> codec 參數 : 消息編解碼器 , 有 444 中實現類型 ; Native 應用端 與 Flutter 中的消息編解碼器也要保持一致 ;
2、使用 BasicMessageChannel 接收 Native 發送的消息
創建好 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)));});}}傳入的參數是 Future<T> handler(T message) , 該參數是用于消息處理的 , 需要配合 BinaryMessenger 進行消息處理 ;
3、使用 BasicMessageChannel 向 Native 發送消息
在 Flutter 端如果想 Native 端發送消息 , 使用 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 方法 參數 / 返回值 分析 :
- T message 參數 : Flutter 端要發送給 Native 端的消息 ;
- Future<T> 返回值 : Native 端回送給 Flutter 端的消息 ;
該 send 方法接收一個 Future<T> 類型返回值 , 該返回值是異步的 ;
也就是說 Dart 端向 Native 端發送一個消息 , Native 端處理完畢后 , 會回傳一個異步消息 ;
4、BasicMessageChannel 使用流程
BasicMessageChannel 使用流程 :
首先 , 導入 Flutter 與 Native 通信 的 Dart 包 ;
import 'package:flutter/services.dart';然后 , 定義并實現 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 發送消息 ;
/// 向 Native 發送消息try {String response = await _basicMessageChannel.send(value);} on PlatformException catch (e) {print(e);}三、相關資源
參考資料 :
- 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 端实现 BasicMessageChannel 通信 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flutter】Flutter 混合开
- 下一篇: 【Flutter】Flutter 混合开