2

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);
    }
  }
}

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
  • The error simply says one of the values is being null here (variables). try printing count, result. my guess is that the count value is getting null somewhere in the code. in your future method put a condition that if count is not null, continue. – Benyamin Sep 05 '21 at 19:32
  • Possible this will help out : https://stackoverflow.com/a/67990442/6280156 – Ankit Tale Sep 05 '21 at 19:32
  • @Benyamin There is no null value here. I try without isolate. by the way I handle null value in native code – BIS Tech Sep 06 '21 at 04:59
  • @Benyamincheck my code. I already handle null – BIS Tech Sep 06 '21 at 05:01
  • @AnkitTale This is on isolate side not null handling – BIS Tech Sep 06 '21 at 05:02

1 Answers1

0

The engine code dereferences null and crashes when attempting to send a platform message from the secondary isolate. I haven't pinpointed exactly where yet; I get a tombstone, but need to learn how to interpret such a thing.

As a (clumsy) workaround, the secondary isolate could ask the primary one to send the messa

I got the same issue. To track this issue, please subscribe https://github.com/flutter/flutter/issues/13937

ke Chankrisna
  • 171
  • 3
  • 4