I tried to clear my app notification count using the platform code. And I try to implement isolate for this function in dart side.
I got the above error message to both compute and isolate ways. Both are failed.
Error
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)
Full Code
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);
}
}
}