Home > OS >  Dart: access nested maps using 'key' and 'value' keywords
Dart: access nested maps using 'key' and 'value' keywords

Time:07-27

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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
 
   }
  • Related