标签化网站,网站服务器需要多少钱,风机 东莞网站建设,上海网站建设公司哪家好在混合开发中避免不了通信#xff0c;简单记录一下#xff0c;Flutter iOS工程与Flutter 之间相互通信。 Flutter中通过Platform Channel实现Flutter和原生端的数据传递#xff0c;是怎么进行数据通信#xff0c;以及怎么配置#xff0c;下面一一进行详解。 
FlutterMetho…在混合开发中避免不了通信简单记录一下Flutter iOS工程与Flutter 之间相互通信。 Flutter中通过Platform Channel实现Flutter和原生端的数据传递是怎么进行数据通信以及怎么配置下面一一进行详解。 
FlutterMethodChannel 使用 注iOS 端简单设置 
class HYFlutterNavChannel: NSObject {objc public static let share  HYFlutterNavChannel()// 声明 FlutterMethodChannelvar channel: FlutterMethodChannel// lazy var map: [String: (_ call: FlutterMethodCall, _ result: FlutterResult) - Void]  {return [pop:pop,]}()override init() {// name 一定需要和  flutter里面约定好保持一致channel  FlutterMethodChannel.init(name: Flutter/navigation, binaryMessenger: FlutterBoost.instance().engine().binaryMessenger)super.init()channel.setMethodCallHandler {[weak self] (call, reslt) inlet method  self?.map[call.method]method?(call, reslt)}}objc public static func start() {_  HYFlutterNavChannel.share}// popfunc pop(call: FlutterMethodCall, result: FlutterResult)  {UINavigationController.topNavigationController()?.navigationController?.popViewController(animated: true)}}在iOS 注册Flutter 引擎的地方使用 
// 案例是放到 AppDelegate中
[FlutterBoost.instance setup:application delegate:delegate callback:^(FlutterEngine *engine) {NSLog(FlutterBoost 开始操作);// 使用 MethodChannel[HYFlutterNavChannel start];[HYFlutterCommonChannel start];}];上述就把iOS端使用FlutterMethodChannel简单进行通信集成完毕。 
Flutter 端 MethodChannel 集成与使用 
import dart:collection;import package:flutter/services.dart;class NavigationChannel {// 这里需要和原生保存一致  Flutter/navigation// ignore: constant_identifier_namesstatic const MethodChannel channel_navigation MethodChannel(Flutter/navigation);// ignore: non_constant_identifier_namesstatic final channel_navigation_handlers HashMapString, MethodCallHandler();NavigationChannel() {init();}void init() {channel_navigation_handlers[nativeQuitFlutter]  nativeQuitFlutter;channel_navigation.setMethodCallHandler((call) async {channel_navigation_handlers[call.method]?.call(call);});}//  native 提供的功能方法Futurevoid finishHostPage() async {return channel_navigation.invokeMethod(pop);}Futurevoid nativeQuitFlutter(MethodCall call) async {}// -------------flutter提供的功能-----------------void registerInitRoute(MethodCallHandler handler) {channel_navigation_handlers[initRoute]  handler;}
}typedef MethodCallHandler  Futuredynamic Function(MethodCall call)?;以上 Flutter MethodChannel 集成完毕 
Flutter 使用MethodChannel 这里使用了一个类进行统一管理 通信类 
import package:my_flutter/common_channel.dart;import navigation_channel.dart;class Channels {// ignore: empty_constructor_bodiesChannels._() {}// 注册 Channelstatic final navigation  NavigationChannel();static final common  CommonChannel();
} 
在Flutter使用的地方进行调用 
Channels.navigation.finishHostPage();上述完成flutter就可以调用原生里面注册的pop方法了。