tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
780 stars 64 forks source link

Serialising object works fine, deserialising fails #352

Closed daveshirman closed 1 year ago

daveshirman commented 1 year ago

Background:

Why is it that my map object, which has a nested List<String> property assetSubTypeNames will serialise fine, but when de-serialising, it's not properly done?

The List<String> property assetSubTypeNames should be a Map at this point. Surely?

Please explain what I'm doing wrong. Thanks!

Note:

If I add the following code between lines 52 + 53:

    for (var item in map['assetTypes'] as List) {
      item['assetSubTypeNames'] = jsonDecode(item['assetSubTypeNames']);
    }

it works fine. But I shouldn't have to.

Error

Unhandled Exception: type 'String' is not a subtype of type 'Iterable<dynamic>'

Screenshot 2023-04-12 at 14 17 08

alextekartik commented 1 year ago

Indeed it seems that assetSubTypeNames is a String not a list since you are encoding it as a string your toJson() method:

'assetsSubTypeNames': jsonEncode(assetSubTypeNames)

My guess is that you should just do:

'assetsSubTypeNames': assetSubTypeNames
daveshirman commented 1 year ago

You are absolutely correct. My apologies. Thanks!