isolateFunctionMethodChannelCommon function
void
isolateFunctionMethodChannelCommon()
Implementation
void isolateFunctionMethodChannelCommon() {
WidgetsFlutterBinding.ensureInitialized();
MethodChannel _methodChannel = new MethodChannel("de.cobinja/FlutterService", JSONMethodCodec());
ReceivePort? localRecPort;
SendPort? localSendPort;
void portListener(message) {
if (message is Map) {
if (message["action"] != null) {
if (message["action"] == "sendData") {
_methodChannel.invokeMethod<bool>("sendData", {"data": message["data"]});
}
}
}
}
_methodChannel.setMethodCallHandler((call) async {
if (call.method == "runCallback") {
SharedPreferences prefs = await SharedPreferences.getInstance();
int? rawHandle = prefs.getInt("cobiFlutterService.callbackHandle");
if (rawHandle == null) {
return false;
}
CallbackHandle cbHandle = CallbackHandle.fromRawHandle(rawHandle);
Function? callback = PluginUtilities.getCallbackFromHandle(cbHandle);
if (callback == null) {
return false;
}
localRecPort = ReceivePort();
SendPort remoteSendPort = localRecPort!.sendPort;
localRecPort!.listen(portListener);
ReceivePort remoteRecPort = ReceivePort();
localSendPort = remoteRecPort.sendPort;
callback(remoteSendPort, remoteRecPort);
return true;
}
if (call.method == "stopService") {
localSendPort?.send("stop");
}
if (call.method == "executeAction") {
String action = call.arguments["action"];
localSendPort?.send({"action": action});
}
if (call.method == "onReceiveData") {
if (call.arguments != null && call.arguments["data"] != null)
localSendPort!.send({
"event": "onReceiveData",
"data": call.arguments["data"]
});
}
});
_methodChannel.invokeMethod<bool>("runCallback");
}