平台通道代码的颤振隔离问题



我尝试使用平台代码清除应用程序通知计数。并且我尝试在dart侧实现这个函数的隔离。

我得到了上面的错误信息来计算和隔离方法。

误差

Exception: Null check operator used on a null value
MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
NotificationCountService.computeFunction (package:yellow/services/notificationCountService.dart:32:16)
_IsolateConfiguration.apply (package:flutter/src/foundation/_isolates_io.dart:81:34)
_spawn.<anonymous closure> (package:flutter/src/foundation/_isolates_io.dart:88:65)
_spawn.<anonymous closure> (package:flutter/src/foundation/_isolates_io.dart:87:5)
Timeline.timeSync (dart:developer/timeline.dart:163:22)
_spawn (package:flutter/src/foundation/_isolates_io.dart:85:35)
_delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:286:17)
_RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

完整代码

class NotificationCountService {
static const platform = MethodChannel('path/notificationCount');
static Future<void> setCountCompute(int count) async {
//await compute(computeFunction, count);
spawnNewIsolate(count);
}
static computeFunction(int count) async {
try {
count = (count ?? 0) <= 0 ? 0 : count;
compute(await platform.invokeMethod('setCount', {'count': count}), );
print('result is $result');
} on PlatformException catch (e) {
print("Failed to get battery level: '${e.message}'.");
return null;
}
}
static void spawnNewIsolate(int count) async {
ReceivePort receivePort = ReceivePort();
try {
await Isolate.spawn(createFunction, receivePort.sendPort);
SendPort childSendPort = await receivePort.first;
ReceivePort responsePort = ReceivePort();
childSendPort.send([count, receivePort.sendPort]);
var response = await responsePort.first;
print('response $response');
} catch (e) {
print("Error: $e");
}
}
static void createFunction(SendPort mainPort) async {
ReceivePort childPort = ReceivePort();
mainPort.send(childPort.sendPort);
await for (var message in childPort) {
int count = message[0];
SendPort replyPort = message[1];
var result = await platform.invokeMethod('setCount', {'count': count});
replyPort.send(result);
}
}
}

引擎代码在试图从次要隔离发送平台消息时取消引用null并崩溃。我还没有确定确切的地点;我得到一个墓碑,但需要学习如何解释这样的事情。

作为一种(笨拙的)解决方法,次要隔离可以要求主要隔离发送消息

我也有同样的问题。要跟踪此问题,请订阅https://github.com/flutter/flutter/issues/13937

最新更新