一、原生和flutter 通信
ios 通信类 CommonUtil.swift
import Foundation
import Flutter
public 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"] = type
res["data"] = data
res["errCode"] = errCode
res["errMsg"] = errMsg
print("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 void emitEvent(String method, String type, Object errCode, String errMsg, T data) {
runMainThread(() -> {
Map 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 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);