1

Given this Map

var theMap = {"first":{"second":{"third": "fourth"}}};

I can access the first key and value like this

for (final mapEntry in theMap.entries) {
print (mapEntry.key); // prints "first"
print (mapEntry.value); //prints "{second: {third: fourth}}"
}

How can I access "second", "third" and "fourth"? I tried this

print ((mapEntry.value as MapEntry).key);

but it throws a TypeError

This works

  for (final mapEntry2 in mapEntry.value.entries) {
    print (mapEntry2.key);  //prints "second"
  }

but it seems rather cumbersome.

0

3 Answers 3

1

This is what I was looking for:

var theMap = {"first":{"second":{"third": "fourth"}}};
  
  for (final mapEntry in theMap.entries) { 
    var firstKey=theMap.keys.first;
    print (firstKey);  //first
    print (theMap[firstKey]); //{second: {third: fourth}}
    var secondKey = theMap[firstKey]?.keys.first;
    print (secondKey); //second
    print (theMap[firstKey]?[secondKey]); //{third: fourth}
    var thirdKey= theMap[firstKey]?[secondKey]?.keys.first;
    print (thirdKey); // third
    print (theMap[firstKey]?[secondKey]?[thirdKey]); //fourth
 
   }
Sign up to request clarification or add additional context in comments.

Comments

0

You theMap dataType is Map<String, Map<String, Map<String, String>>>.

You can directly get inner map like

theMap["first"]?["second"]
void main(List<String> args) {
  Map<String, Map<String, Map<String, String>>> theMap = {
    "first": {
      "second": {"third": "fourth"}
    }
  };
  print(theMap["first"]); //{second: {third: fourth}}
  print(theMap["first"]?.keys.toString()); // set:=> second
  print(theMap["first"]?.values.toString());// set=> {third: fourth} 
  print(theMap["first"]?["second"]); //{third: fourth}
}

Or like

void main(List<String> args) {
  Map<String, Map<String, Map<String, String>>> theMap = {
    "first": {
      "second": {"third": "fourth"}
    }
  };

  theMap.forEach((key, value) {
    // for top level (first)
    print("First layer key: $key");
    print("First layer value: $value");
    value.forEach((key, value) {
      //second layer
      print("Second layer key: $key");
      print("Second layer value: $value");

      value.forEach((key, value) {
        print("Thired layer key:  $key");
        print("Thired layer value:  $value");
      });
    });
  });
}

And result

First layer key: first
First layer value: {second: {third: fourth}}
Second layer key: second
Second layer value: {third: fourth}
Thired layer key:  third
Thired layer value:  fourth

3 Comments

Yes, that's similar to the example I gave in the question, in that it loops through all of the entries. I was wondering if there is a way to get the key and value of the inner Maps without using a loop. If I have a single entry in theMap can I access the nested keys and values without using a loop?
if it is only one you use .first also theMap["first"]?.keys provide the things you are looking for? or try ` print(theMap["first"]);` to get {second: {third: fourth}}
Also you can use Map.from(mapEntry.value); to get the map
0

you need to use a recursive function like this:

readItAll(var x)
{
   if(x.runtimeType == String){
      print(x);
     return;
   }
     
    for (final mapEntry in x.entries) {
       print(mapEntry.key);

    readItAll (mapEntry.value);
  }
}

where for this call:

var theMap = {"first":{"second":{"third": "fourth"}}};
  
readItAll(theMap);

the output would be:

first
second
third
fourth

1 Comment

Please don't do this: x.runtimeType == String. Instead, use x is String. If it's true, then x is temporarily promoted to String type. Your way won't do that.

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.