I am trying to de-serialize json in a parent class. One of my items is a Map of a Map, which I'd like to de-serialize into a Types class. I am struggling with parsing this because of the nested map in a map and I'm unclear on the proper syntax. At the end of the day I want a List in the parent class. The items within the types json block are dynamic, so there could be a type of critical, notice, etc. with varying descriptions.
Json sample:
{
"types":{
"alert":{
"description":"Action item."
},
"question":{
"description":"Select an applicable response."
},
"info":{
"description":"This is an information message, no action required."
}
}
}
Types class:
class Types {
final String name;
final String description;
Types({
required this.name,
required this.description,
});
factory Types.fromJson(String id, Map<String, dynamic> json) {
return Types(
name: id,
description: json['description'] == null ? '' : json['description'],
);
}
}
Parent class:
class Parent {
final String id;
final String name;
final String description;
final Features features;
final List<Types> types;
final List<String> users;
Parent({
required this.id,
required this.name,
required this.description,
required this.features,
required this.types,
required this.users,
});
factory Parent.fromJson( Map<String, dynamic> json) {
return Parent(
id: json['id'] == null ? '' : json['id'],
name: json['name'] == null ? '' : json['name'],
description: json['description'] == null ? '' : json['description'],
features: json['features'] == null
? Features()
: Features.fromJson(json['features']),
types: json['types'] == null ? [] : // ??? How to deserialize Map{Map{}} ,
users: json['users'] == null ? [] : List<String>.from(json['users']),
);
}
}
Any and all help is appreciated. Likewise if there is a better way to store this data I am open to that. The types class allows me to add future fields to it if necessary.
Thank you.