11

Can anyone tell me whats wrong in this code?

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)

9
  • this the official doc which I have followed. They are saying we can only pass primitive data type but I am getting Error with the similar code. Commented Mar 9, 2022 at 8:28
  • Even this code also generating the same problem. void onPressed() async { await Isolate.spawn(gotoNext, "OK"); } void gotoNext(String args) { log(args); } Commented Mar 9, 2022 at 8:32
  • where are you sending Path object? Commented Mar 9, 2022 at 8:46
  • I am not sending bro. Thats why I am also unable to track the root cause of the problem. I have simply calling the onPressed Function on a TextButton Tap. Commented Mar 9, 2022 at 8:51
  • >E/flutter (12519): [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) E/flutter (12519): #0 Isolate._spawnFunction (dart:isolate-patch/isolate_patch.dart:395:25) E/flutter (12519): #1 Isolate.spawn (dart:isolate-patch/isolate_patch.dart:375:7) E/flutter (12519): #2 _Screen1State.onPressed (package:practice/screen_1.dart:32:19) E/flutter (12519): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21) Commented Mar 9, 2022 at 8:55

4 Answers 4

32

Hii There is a mistake in your code. As far as I know from the official documentation The callback method for an Isolate should be a top level function or a static method. So there are two solution for this problem.

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);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes Now it is working for me Thanks!
Good to hear that.
4

I had the same issue today. Turns out the code was not supposed to run inside a widget class, the examples use them outside. Let me know how it goes.

1 Comment

Exactly you are right! It is working in main thread but not from a Widget class. void main() async { runApp(MyApp()); var receivePort = ReceivePort(); await Isolate.spawn(gotoNext, [receivePort.sendPort]); final msg = await receivePort.first; print('received >$msg<'); } void gotoNext(List<dynamic> args) { SendPort sendPort = args[0]; print('>input parameters: $args<'); Isolate.exit(sendPort, "OK"); }
0

arg is List<dynamic> not List<SendPort> so code is not valid

SendPort sendPort = args[0];

please try, if arg[0] is SendPort:

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

10 Comments

But List<SendPort> will not be the primitive type? Also I have tried to Pass Map, String and int value but problem was remains.
check this documentation provided here.
pls try: ``` SendPort sendPort = args[0] as SendPort; ```
This was an Unnecessary cast bro. Also Not working.
@VarunVerma see my edit
|
0

In my case, The reason is because I passed argument Object too complicated to heavy task.

 final isolate = await Isolate.spawn<List<dynamic>>(
    getIncidentListIsolate,
    [receivePort.sendPort, _affairService]);

_affairService is one complicated object. I replace with String, it it works fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.