Search code examples
flutterhashmapflutter-test

Not getting desired output when trying to add values to a map Flutter


I have a map named _body to which I'm trying to add values dynamically using

for(i=0;i<n;i++)
    {
    _body.addAll({
            'receiver_details': {

              '[$i]': {
                "receiver_name": "$receiver_name",
              }
    }
}
)
}

the desired output would be

{receiver_details: {[0]: {receiver_name: abc}},[1]: {receiver_name: vhhh}}}

but all I'm getting is

{receiver_details: [1]: {receiver_name: vhhh}}}

Only the last index is being added and the rest are getting overwritten. Can anyone please tell me what I'm doing wrong? Thanks!


Solution

  • Try this

    _body['receiver_details'] = {};
    for(i=0;i<n;i++)
    {
      _body['receiver_details']['[$i]'] = {
            "receiver_name": "$receiver_name",
      };
    }