Converting object to an encodable object failed: _linkedhashmap len:1 Flutter

Issue

I have a map which contains two standard key and value pairs like

_body = {
          "expected_time": "$Time",
          "payment": "$payment",
          "receiver" : {},
};

And another map named receiver inside it as you can see. Values are being passed to the receiver later using a for loop and the information is being added just fine.

for(int i=0; i<=n; i++)
{
  _body['receiver'][i] = {

            "receiver_name" : "abc",
};
}

The issue I’m facing is when trying to send this map to an api call via http.post
there jsonEncode(body) has been used to encode the map to send it. When sending those simple key value pairs I’m getting no error but when I’m trying to include the receiver field as well I’m getting the error "Converting object to an encodable object failed: _linkedhashmap len:1".

Can anyone please tell me what I need to here? Thanks!

Solution

you are not making it in the right way, try this

var _body = {
 "expected_time": "time",
 "payment": "payment",
 "receiver" : {},
};

for(int i=0; i<=3; i++) {
 _body.addAll({
  'receiver[$i]': {
    "receiver_name": "abc",
  }
 });
}
print(_body);

and the output is like this

{expected_time: time, payment: payment, receiver: {}, receiver[0]: {receiver_name: abc}, receiver[1]: {receiver_name: abc}, receiver[2]: {receiver_name: abc}, receiver[3]: {receiver_name: abc}}

you can now encode it

Answered By – Ahmed Mohamed

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *