0

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?

2
  • tagDict is defined as a Map using String as key type and Object as value type. So Dart can only guarantee that objects fetched from your map is of the type Object which 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. Commented Sep 9, 2020 at 16:20
  • @julemand101 Do you have any suggestion how to this? Actually I was already quite "proud" to get so far in flutter (in swift this is a piece of cake). What do you mean with a more specific value type. I thought with a well defined class with fixed value types it is already quite specific. Commented Sep 9, 2020 at 16:32

1 Answer 1

2

You see such error because you have declared tagDict's type in TaskTags class as Map<String, Object>. So when you retrieve an item from tagDict like listOfTags.tagDict["abc"], Dart identifies that item type as an Object, and the Object type has no helpHdr field; Thus you face that error.

In order to fix this you need to change tagDit's type in TaskTags class to Map<String, TaskTag>.

Sign up to request clarification or add additional context in comments.

5 Comments

Arg, this was my tagDict definition before I tried to fix the previous issue. Thanks a lot! If I may ask, what is the difference between Map<String, TaskTag>(); and <String, Object>{} (I am still struggling with some flutter syntax details)?
Same thing but shorter. {} can both define a Set or Map which are determined based on the number of specified generics. So, <String>{} is a Set of String and <String, int>{} is a Map with String as key type and int as value type. :)
@julemand101 Both expressions might be quite close, but there has to be some difference. With < , >{} the code failed, with Map< , > it works
@w461 Are you sure? There's no difference between <String, TaskTag>{} and Map<String, TaskTag>(). Your problem was about the type of the Map value which used to be Object instead of TaskTag.
Aaaaaah!!! Apologies. I stop for today, I guess my eyes are getting fuzzy. I haven't noticed that when I copied the other notation into my code for the previous issue (when I hoped to somehow remove a List<TaskTags> vs TaskTags issue, which obviously had a different cause). Thanks a lot guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.