dart-archive / isolate

Makes working with Dart isolates easier.
https://pub.dev/packages/isolate
BSD 3-Clause "New" or "Revised" License
90 stars 34 forks source link

i cant pass a function parameter with isolate.spawn and cant pass more than one parameters at time #49

Closed Shiberal closed 3 years ago

Shiberal commented 3 years ago

dart throws an exception that doesn't allow me to pass a function type through isolate.spawn. ArgumentError (Invalid argument(s): Illegal argument in isolate message : (object is a FunctionType))

client is Socket type from socket.io dart library isolate = await Isolate.spawn(isolateClient, client);

https://pub.dev/packages/socket_io <-- this library.

so.. i need to listen for a connection and create a thread using isolate to 'isolate the client operations' but it seems like isolate was made just for math stuff in mind.

i also tried passing a List but... i still cant pass this client that seems like a FunctionType

lrhn commented 3 years ago

You cannot pass a function expression value, only a top-level or static function tear-off. Putting the closure into another object doesn't help.

What you can do is to create a class. Instead of using (double x, bool y) { some code } as a function expression, perhaps closing over local variables v and w, you can make a helper class:

class _LocalHelper {
  final int v;
  final String w;
  _LocalHelper(this.v, this.w);
  int call(double x, bool y) { some code }
}

and then send _LocalHelper(v, w) as an object instead of the function.