未处理的异常:无效参数:隔离消息中的非法参数:(对象扩展 NativeWrapper - 库:'dart:ui' Class:路径)

Unhandled Exception: Invalid argument(s): Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:ui' Class: Path)

谁能告诉我这段代码有什么问题吗?

void onPressed() async {
    //Navigator.pushNamed(context, "/screen2", arguments: []);
    var receivePort = ReceivePort();
    await Isolate.spawn(gotoNext, [receivePort.sendPort]);
    final msg = await receivePort.first;
    print(msg);
  }

  void gotoNext(List<dynamic> args) {
    SendPort sendPort = args[0];
    log(args.toString());
    Isolate.exit(sendPort, "OK");
  }

E/flutter (12062): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument(s): Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:ui' Class: Path)

arg 是 List 而非 List,因此代码无效

SendPort sendPort = args[0];

请尝试,如果 arg[0] 是发送端口:

void _readAndParseJson(List<dynamic> args) async {
  SendPort? responsePort;
  final dynamic _responsePort = args[0];

  if (_responsePort != null && _responsePort is SendPort) {
    responsePort = _responsePort;
  }
  String? fileName;
  final dynamic _fileName = args[1];
  if (_fileName != null && _fileName is String) {
    fileName = _fileName;
  }
  final fileData = await File(fileName).readAsString();
  final result = jsonDecode(fileData) as Map<String, dynamic>;
  Isolate.exit(responsePort, result);
}

我今天遇到了同样的问题。事实证明代码不应该 运行 在小部件 class 内,示例在外部使用它们。让我知道进展如何。

您好,您的代码有误。据我从官方文档中了解到,Isolate 的回调方法应该是 top level functionstatic 方法。 所以这个问题有两种解决方法。

Solution 1. declare callback function as top level function.

class MyHomePage extends StatelessWidget {
  final String title;

  const MyHomePage({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: TextButton(
        child: const Text("Run Isolate"),
        onPressed: _onPressed,
      ));
    );
  }

  // Callback function for Text Button Event this should be a class member
  void _onPressed() async {
    var receivePort = ReceivePort();
    // Here runMyIsolate methos should be a top level function
    await Isolate.spawn(runMyIsolate, [receivePort.sendPort, "My Custom Message"]);
    print(await receivePort.first);
  }
}


// We declare a top level function here for an isolated callback function
void runMyIsolate(List<dynamic> args) {
  var sendPort = args[0] as SendPort;
  print("In runMyIsolate ");
  Isolate.exit(sendPort, args);
}

Solution 2. instead this top level function we can declare this function as a static function for the same class, consider below example.

    class MyHomePage extends StatelessWidget {
  final String title;

  const MyHomePage({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: TextButton(
        child: const Text("Run Isolate"),
        onPressed: _onPressed,
      ));
    );
  }

  // Callback function for Text Button Event this should be a class member
  void _onPressed() async {
    var receivePort = ReceivePort();
    // Here runMyIsolate methos should be a top level function
    await Isolate.spawn(runMyIsolate, [receivePort.sendPort, "My Custom Message"]);
    print(await receivePort.first);
  }
  
  // We declare a static function here for an isolated callback function
  static void runMyIsolate(List<dynamic> args) {
    var sendPort = args[0] as SendPort;
    print("In runMyIsolate ");
    Isolate.exit(sendPort, args);
  }
}