2
votes

Trying to parse this json:

"items":{
    "A101": { "uID": "11", "count": 1 },
    "A102": { "uID": "12", "count": 2 }
}

Using the following code:

Map<String, ItemData> items = (json['items'] as Map).map((k, v) => MapEntry(k, ItemData.fromJson(v)));

Where ItemData Factory is:

factory ItemData.fromJson(Map<String, dynamic> json){
    return ItemData(
        uID : json['uID'],
        count : json['count']
    );
}

And I get the following error:

type '_InternalLinkedHashMap< dynamic, dynamic>' is not a subtype of type 'Map< String, dynamic>'

It's important to mention that this code works just fine when parsing the same json from a local file. Seems like there's a problem with getting the data from Firestore

1

1 Answers

1
votes

Figured it out, it should be:

Map< String, ItemData> items = ((json['items'] as Map).cast< String, dynamic>()).map((k, v) => MapEntry(k, ItemData.fromJson(v.cast< String, dynamic>())));