Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error when using JsonSerializable on web #15

Closed
Run10Break1 opened this issue Dec 6, 2022 · 2 comments
Closed

error when using JsonSerializable on web #15

Run10Break1 opened this issue Dec 6, 2022 · 2 comments

Comments

@Run10Break1
Copy link

recently, I stuck with a problem on the web. below is an error.

error: ["$W","TypeError: Instance of 'minified:z<dynamic>': type 'minified:z<dynamic>' is not a subtype of type 'List<double>'","TypeError: Instance of 'minified:z<dynamic>': type 'minified:z<dynamic>' is not a subtype of type 'List<double>'\n    at Object.b (http://localhost:62206/workers/neverland_worker.dart.js:282:3)\n    at Object.i4 (http://localhost:62206/work...

I figure out that the error comes from type casting on .g.dart, it changes from weak type (important, JS doesn't have a generic type) to strict type like List<double>.

size: const SizeConverter().fromJson(json['size'] as List<double>),

so, when you have an object containing other objects and want to encode the object on the Worker class and decode it on the Service class using code generated by JsonSerializable, change the type to dynamic.

below is an example.

class SizeConverter implements JsonConverter<Size, List<double>> {

  const SizeConverter();

  @override
  Size fromJson(List<double> json) {
    return Size(json[0], json[1]);
  }

  @override
  List<double> toJson(Size object) {
    return [object.width, object.height];
  }
}

on the Web, there isn't a type like List<double>, only List; so change like this.

class SizeConverter implements JsonConverter<Size, dynamic> {

  const SizeConverter();

  @override
  Size fromJson(dynamic json) {
    return Size(json[0], json[1]);
  }

  @override
  dynamic toJson(Size object) {
    return [object.width, object.height];
  }
}

I know that README also says you should take care when using JsonSerializable, but I want to show another case.

@d-markey
Copy link
Owner

d-markey commented Dec 6, 2022

Hello,

Thanks for your feedback. That's right, types such as Map<K,V>/List<T> loose their specialization when crossing worker boundaries on Web platforms.

Using dynamic is one way to go. I believe you could also use a bare List. Using List<whatever> is doomed because whatever will not come through from the caller to the worker!

Also, have you tried using https://pub.dev/packages/squadron_builder? It should help you get rid of some boilerplate code as well as handle type considerations. The code generator tries its best to minimize side effects related to the lack of generics when communicating with a Web Worker. Note that I've never tested it with JsonConverter. So do not hesitate to file an issue if you have problems squadron_builder :-)

Closing this issue for now.

@d-markey d-markey closed this as completed Dec 6, 2022
@Run10Break1
Copy link
Author

thank you, I will try squadron_builder

ps, I really appreciate you for making Squadron : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants