0

I am Trying To Pass List Of Objects To Isolate.spawan Method Using The Code Below

  List<myContactsModel> sl=[myContactsModel(name:'ABcde',age:'34')];
  var sls=jsonEncode(sl.toString());
  Map map={
    "sendPort":receivePort.sendPort,
      "listContacts":sls,
  };
      
  isolate = await Isolate.spawn(sayHello,map);

The Map passes fine As A Parameter To sayHello function.

static void sayHello(Map map) async{
var sp=map['sendPort'];
List<myContactsModel> listContacts=jsonDecode(map['listContacts']);
sp.send("Hello from Isolate");
}

But When I Try To Get The Value of listContacts From Map And Store It In List listContacts it shows followinf error

Unhandled exception:
type 'String' is not a subtype of type 'List<myContactsModel>'
Benson OO
  • 477
  • 5
  • 22
  • this is because you are encoding a `String` not a `List`, i mean: `var sls=jsonEncode(sl.toString());` – pskink Oct 13 '21 at 08:57
  • @pskink i knw that if i dont covert it to string before encoding the isolate doesn't allow me to pass it as parameter – Benson OO Oct 13 '21 at 08:59
  • https://stackoverflow.com/a/57712680/7758318 this may help – Balaji Oct 13 '21 at 09:00
  • `jsonEncode()` returns a `String` but it can take any `Object` - a `List` for example, your problem is that you are passing a `String` to `jsonEncode` - check this code snippet: `void main() { final list = [1,2,3]; print(json.encode(list)); print(json.encode(list.toString())); print(json.encode(list).codeUnits); print(json.encode(list.toString()).codeUnits); }` – pskink Oct 13 '21 at 09:05
  • @pskink i got that Thanks For Pointing out That Silly Mistake – Benson OO Oct 13 '21 at 09:10

0 Answers0