Failing to send a serialized freezed object over a port to another isolate

36 views Asked by At

I have a freezed class in my Flutter app. I'm serializing it to a Map and trying to send it over a port to another isolate.

Receiving this weird error:

E/flutter (23696): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument: is a regular instance reachable via  <- _List len:16 (from dart:core)
E/flutter (23696):  <- _Map len:7 (from dart:collection)
E/flutter (23696):  <- _List len:32 (from dart:core)
E/flutter (23696):  <- _Map len:12 (from dart:collection)
E/flutter (23696):  <- _List len:8 (from dart:core)
E/flutter (23696):  <- _Map len:3 (from dart:collection)
E/flutter (23696):  <- _List len:8 (from dart:core)
E/flutter (23696):  <- _Map len:2 (from dart:collection)
E/flutter (23696): : Instance of 'EqualUnmodifiableListView<bool>'
E/flutter (23696): #0      _SendPort._sendInternal (dart:isolate-patch/isolate_patch.dart:249:43)
E/flutter (23696): #1      _SendPort.send (dart:isolate-patch/isolate_patch.dart:230:5)

This error goes away when I remove a List in my class. I wonder why this is happening.

1

There are 1 answers

0
Gazihan Alankus On

This is happening because freezed uses a weird list type (EqualUnmodifiableListView) and port is not able to send it somehow. The error message is not helping very much.

The following code helps fix the issue:

// in the freezed class, the field: 
    @Default([
            false,
          ])
    @JsonKey(
      toJson: _boolListToString,
      fromJson: _boolListFromString,
    )

// the two functions used:
String _boolListToString(List<bool> l) {
  return l.map((e) => e ? '1' : '0').join(',');
}

List<bool> _boolListFromString(String s) {
  return s.split(',').map((e) => e == '1').toList();
}