【问题标题】:Flutter Isolate Issue with Platform Channel code平台通道代码的颤振隔离问题
【发布时间】:2021-11-03 01:41:56
【问题描述】:

我尝试使用平台代码清除我的应用通知计数。我尝试在飞镖端为这个功能实现隔离。

我收到了上述计算和隔离方式的错误消息。两者都失败了。

错误

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

【问题讨论】:

  • 错误只是说这里的值之一为空(变量)。尝试打印计数,结果。我的猜测是计数值在代码中的某处变为空。在你未来的方法中设置一个条件,如果 count 不为空,继续。
  • 这可能会有所帮助:stackoverflow.com/a/67990442/6280156
  • @Benyamin 这里没有空值。我尝试不隔离。顺便说一句,我在本机代码中处理 null 值
  • @Benyamin 检查我的代码。我已经处理了 null
  • @AnkitTale 这是在隔离端而不是空处理

标签: flutter dart dart-isolates


【解决方案1】:

引擎代码在尝试从辅助隔离区发送平台消息时取消引用 null 并崩溃。我还没有确定确切的位置;我得到了一块墓碑,但需要学习如何解释这样的事情。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-02
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多