0

I have a Map<String, dynamic> of the structure {'a': data, 'b': {'c': {'key':data}}}.

I want to use a for loop to add additional key value pairs to 'b' but can't find a way to do so. addAll overwrites 'b' during every iteration which is the behavior of addAll provided there is an existing key...anyone have ideas on how this can be accomplished?

My other thought on how to do this is to create a new map secondMap. This is able to create a map without key value pairs being overwritten...but then I am stuck on how I can combine the secondMap to the myMap under 'b'

  for (var i = 0; i <= info.length - 1; i++) {
          myMap.addAll({'b': {info[i].name: {'key': data}
                      }});
//method 2
  for (var i = 0; i <= info.length - 1; i++) {
          secondMap.addAll({info[i].name: {'key': data}}
                      );
                  

1 Answer 1

1

I hope this sample code helps you.

void main() {
  
 final Map<String, dynamic> map = {'a': "data", 'b': {}};
  
  for (int i = 0; i < 10 ; i++) {
    map['b'][i.toString()] = {"key": i};
  }
  
  print(map);
}
Sign up to request clarification or add additional context in comments.

Comments

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.