当前位置:首页 > 编程笔记 > 正文
已解决

flutter与原生 相互通信实战

来自网友在路上 147847提问 提问时间:2023-10-21 16:36:35阅读次数: 47

最佳答案 问答题库478位专家为你答疑解惑

一、原生和flutter 通信

ios 通信类 CommonUtil.swift 

import Foundation
import Flutterpublic class CommonUtil {public static func emitEvent(channel: FlutterMethodChannel, method: String, type: String, errCode: Int32?, errMsg: String?, data: Any?){safeMainAsync {var res: [String: Any] = [:]res["type"] = typeres["data"] = datares["errCode"] = errCoderes["errMsg"] = errMsgprint("native call flutter { method: \(method) type: \(type) }")channel.invokeMethod(method, arguments: res)}}
}

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===================="));

package chat.konnect.konnect_im_sdk.util;import android.os.Handler;
import android.os.Looper;
import androidx.collection.ArrayMap;
import java.util.Map;
import io.flutter.Log;
import io.flutter.plugin.common.MethodChannel;
import chat.konnect.konnect_im_sdk.KonnectImSdkPlugin;public class CommonUtil {private final static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());public static void runMainThreadReturn(final MethodChannel.Result result, final Object param) {MAIN_HANDLER.post(() -> result.success(param));}public static void runMainThread(Runnable runnable) {MAIN_HANDLER.post(runnable);}public static void runMainThreadReturnError(final MethodChannel.Result result, final String errorCode, final String errorMessage, final Object errorDetails) {MAIN_HANDLER.post(() -> result.error(errorCode, errorMessage, errorDetails));}public static void runMainThreadReturnError(final MethodChannel.Result result, final long errorCode, final String errorMessage, final Object errorDetails) {runMainThreadReturnError(result, String.valueOf(errorCode), errorMessage, errorDetails);}public synchronized static <T> void emitEvent(String method, String type, Object errCode, String errMsg, T data) {runMainThread(() -> {Map<String, Object> res = new ArrayMap<>();if (null != type) {res.put("type", type);}if (null != data) {res.put("data", data);}if (null != errCode) {res.put("errCode", errCode);}if (null != errMsg) {res.put("errMsg", errMsg);}Log.i("IMSDK(native call flutter ===> emitEvent )", "{ method:" + method + ",  type:" + type + " }");KonnectImSdkPlugin.channel.invokeMethod(method, res);});}public static <T> void emitEvent(String method, String type, T data) {emitEvent(method, type, null, null, data);}
}

java调用与flutter通信

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

flutter 发送消息

static const _channel = MethodChannel('konnect_im_sdk');static final iMManager = IMManager(_channel);
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"flutter与原生 相互通信实战":http://eshow365.cn/6-20933-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!