当前位置:   article > 正文

Flutter开发笔记18 - iOS通过FlutterEventChannel发送消息给Flutter_flutter ios fluttereventchannel

flutter ios fluttereventchannel

主要逻辑:

  1. Flutter端 EventChannel监听Native传来的消息
  2. Native通过创建EventChannel,给Flutter层发送消息

代码梳理

1、Flutter端创建EventChannel进行监听

  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. class FlutterJlBluetoothPlugin {
  4. // 工厂模式
  5. factory FlutterJlBluetoothPlugin() =>_getInstance();
  6. static FlutterJlBluetoothPlugin get instance => _getInstance();
  7. static FlutterJlBluetoothPlugin? _instance;
  8. MethodChannel _methodChannel;
  9. EventChannel _eventChannel;
  10. FlutterJlBluetoothPlugin._internal(this._methodChannel, this._eventChannel) {
  11. _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  12. }
  13. static FlutterJlBluetoothPlugin _getInstance() {
  14. if (_instance == null) {
  15. MethodChannel methodChannel = MethodChannel('flutter_jl_bluetooth_plugin');
  16. EventChannel eventChannel = EventChannel('flutter_jl_bluetooth_plugin_event');
  17. _instance = new FlutterJlBluetoothPlugin._internal(methodChannel, eventChannel);
  18. }
  19. return _instance!;
  20. }
  21. /// 获取版本方法
  22. Future<String> get platformVersion async {
  23. final String version =
  24. await _methodChannel.invokeMethod('getPlatformVersion');
  25. return version;
  26. }
  27. /// 数据接收
  28. static void _onEvent(dynamic value) {
  29. print("flutter_jl_bluetooth_plugin_event _onEvent: $value");
  30. }
  31. /// 数据接收: 错误处理
  32. static void _onError(dynamic value) {
  33. print("flutter_jl_bluetooth_plugin_event _onError: $value");
  34. }
  35. }

说明:对于receiveBroadcastStream建议添加名称作为native中传递数据的参数使用


2、iOS端通过Event向Flutter发送数据

2.1 初始化与实现 FlutterStreamHandler的代理方法

  1. #import "FlutterJlBluetoothPlugin.h"
  2. #import "MJExtension.h"
  3. @interface FlutterJlBluetoothPlugin() <FlutterStreamHandler>
  4. @property (nonatomic, strong) FlutterEventSink eventSink;
  5. @end
  6. @implementation FlutterJlBluetoothPlugin
  7. + (instancetype)sharedInstance {
  8. static FlutterJlBluetoothPlugin *instance = nil;
  9. static dispatch_once_t onceToken;
  10. dispatch_once(&onceToken, ^{
  11. instance = [[self alloc] init];
  12. });
  13. return instance;
  14. }
  15. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  16. FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"flutter_jl_bluetooth_plugin" binaryMessenger:[registrar messenger]];
  17. FlutterJlBluetoothPlugin *instance = [FlutterJlBluetoothPlugin sharedInstance];
  18. [registrar addMethodCallDelegate:instance channel:channel];
  19. FlutterEventChannel *eventChannel = [FlutterEventChannel eventChannelWithName:@"flutter_jl_bluetooth_plugin_event" binaryMessenger:[registrar messenger]];
  20. [eventChannel setStreamHandler:instance];
  21. }
  22. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  23. if ([@"getPlatformVersion" isEqualToString:call.method]) {
  24. result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  25. } else {
  26. result(FlutterMethodNotImplemented);
  27. }
  28. }
  29. #pragma mark - FlutterStreamHandler
  30. - (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments eventSink:(FlutterEventSink)eventSink {
  31. self.eventSink = eventSink;
  32. return nil;
  33. }
  34. - (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
  35. self.eventSink = nil;
  36. return nil;
  37. }
  38. @end

2.2 最后是native端调用

if (self.eventSink) self.eventSink([NSDictionary new]);


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/383298
推荐阅读
相关标签
  

闽ICP备14008679号