• flutter与原生 相互通信实战


    一、原生和flutter 通信

    ios 通信类 CommonUtil.swift 

    1. import Foundation
    2. import Flutter
    3. public class CommonUtil {
    4. public static func emitEvent(channel: FlutterMethodChannel, method: String, type: String, errCode: Int32?, errMsg: String?, data: Any?){
    5. safeMainAsync {
    6. var res: [String: Any] = [:]
    7. res["type"] = type
    8. res["data"] = data
    9. res["errCode"] = errCode
    10. res["errMsg"] = errMsg
    11. print("native call flutter { method: \(method) type: \(type) }")
    12. channel.invokeMethod(method, arguments: res)
    13. }
    14. }
    15. }

    swift调用flutter 进行通信 

    CommonUtil.emitEvent(channel: channel, method: "conversationListener", type: "onConversationChanged", errCode: nil, errMsg: nil, data: conversationList)

    安卓 通信类 CommonUtil.java 

    发送

    _channel

    .invokeMethod(

    'initSDK',

    _buildParam(

    {

    "platform": platform,

    "api_addr": apiAddr,

    "ws_addr": wsAddr,

    "data_dir": dataDir,

    "log_level": logLevel,

    "object_storage": objectStorage,

    "operationID": Utils.checkOperationID(operationID),

    },

    ))

    .then(

    (value) => print("===================$value===================="));

    1. package chat.konnect.konnect_im_sdk.util;
    2. import android.os.Handler;
    3. import android.os.Looper;
    4. import androidx.collection.ArrayMap;
    5. import java.util.Map;
    6. import io.flutter.Log;
    7. import io.flutter.plugin.common.MethodChannel;
    8. import chat.konnect.konnect_im_sdk.KonnectImSdkPlugin;
    9. public class CommonUtil {
    10. private final static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
    11. public static void runMainThreadReturn(final MethodChannel.Result result, final Object param) {
    12. MAIN_HANDLER.post(() -> result.success(param));
    13. }
    14. public static void runMainThread(Runnable runnable) {
    15. MAIN_HANDLER.post(runnable);
    16. }
    17. public static void runMainThreadReturnError(final MethodChannel.Result result, final String errorCode, final String errorMessage, final Object errorDetails) {
    18. MAIN_HANDLER.post(() -> result.error(errorCode, errorMessage, errorDetails));
    19. }
    20. public static void runMainThreadReturnError(final MethodChannel.Result result, final long errorCode, final String errorMessage, final Object errorDetails) {
    21. runMainThreadReturnError(result, String.valueOf(errorCode), errorMessage, errorDetails);
    22. }
    23. public synchronized static void emitEvent(String method, String type, Object errCode, String errMsg, T data) {
    24. runMainThread(() -> {
    25. Map res = new ArrayMap<>();
    26. if (null != type) {
    27. res.put("type", type);
    28. }
    29. if (null != data) {
    30. res.put("data", data);
    31. }
    32. if (null != errCode) {
    33. res.put("errCode", errCode);
    34. }
    35. if (null != errMsg) {
    36. res.put("errMsg", errMsg);
    37. }
    38. Log.i("IMSDK(native call flutter ===> emitEvent )", "{ method:" + method + ", type:" + type + " }");
    39. KonnectImSdkPlugin.channel.invokeMethod(method, res);
    40. });
    41. }
    42. public static void emitEvent(String method, String type, T data) {
    43. emitEvent(method, type, null, null, data);
    44. }
    45. }

    java调用与flutter通信

    CommonUtil.emitEvent("userListener", "onSelfInfoUpdated", s);

    flutter 发送消息

    1. static const _channel = MethodChannel('konnect_im_sdk');
    2. static final iMManager = IMManager(_channel);
  • 相关阅读:
    计蒜客详解合集(1)期
    2024/2/18 图论 最短路入门 dijkstra 2
    ZooKeeper的ZAB协议?
    用孙子兵法的智慧指导数据安全工作
    开发测试平台难吗?
    Linux内存管理(四):内存架构和内存模型简述
    浅谈餐饮业油烟污染现状及在线监测系统的设计与应用
    多种方式解决交叉编译中glibc版本不兼容导致的编译问题(libc.so.6: version `GLIBC_xxx‘ not found问题)
    javaweb汽车租赁系统
    剑指offer 38:字符串的排列
  • 原文地址:https://blog.csdn.net/weixin_43575775/article/details/133912302