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}}
);
CodePudding user response:
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);
}