I am trying to get my data out of a dictionary, but somehow nothing works.
I have the following dictionary structure:
class TaskTags {
var tagDict = <String, Object>{};
TaskTags({
this.tagDict,
});
factory TaskTags.fromJson(Map<String, dynamic> json){
var innerMap = json['tagDict'];
var tagMap = Map<String, TaskTag>();
innerMap.forEach((key, value) {
tagMap.addAll({key: TaskTag.fromJson(value)});
});
return new TaskTags(
tagDict: tagMap,
);
}
}
class TaskTag {
String helpHdr;
String helpText;
TaskTag({
this.helpHdr,
this.helpText,
});
factory TaskTag.fromJson(Map<String, dynamic> json){
return new TaskTag(
helpHdr: json['helpHdr'],
helpText: json['helpText'],
);
}
}
This matches to JSON data such as (where abc and def are dictionary keys)
{
"tagDict" : {
"abc" : {
"helpHdr" : "short text1",
"helpText" : "long text1"
},
"def" : {
"helpHdr" : "short text2",
"helpText" : "long text2"
}
}
}
This is stored in a variable listOfTags. In the debugger, it has a structure of
listOfTags
tagDict
0:key="abc"
value= helpHdr = "..."
helpText = "..."
1: key="def"
value= helpHdr = "..."
helpText = "..."
Now I wanted to get the data out with
header: listOfTags.tagDict["abc"].helpHdr,
Doesn't work, I also get no appropriate suggestions when typing the . after the ]. I guess, I have to approach this slightly different - but how?
tagDictis defined as a Map usingStringas key type andObjectas value type. So Dart can only guarantee that objects fetched from your map is of the typeObjectwhich is the reason for the missing auto completion. You code will also not run since Dart cannot statically determine if your code is sound to run. You need to add some type casts or change the value type to something more specific.