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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

文章目錄

  • 一、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 通信 )的全部內容,希望文章能夠幫你解決所遇到的問題。

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