Home > Blockchain >  How to pass this type of data in Post Api Call inFlutter?
How to pass this type of data in Post Api Call inFlutter?

Time:10-29

I am trying to pass the body to post api which is like below:

    {
      "firstname": "Demo",
      "lastname": "User",
      "email": "[email protected]",
      "password": "password",
      "confirm": "password",
      "telephone": "1-541-754-3010",
      "customer_group_id": 1,
      "agree": 1,
      "custom_field": {
        "account": {
          "1": " 364545454"
        }
      }
    }

and my problem is how to pass this section.

    "custom_field": {
        "account": {
          "1": " 364545454"
        }
      }

if am trying to pass in

Map<Strng,dynamic>` data = 

    {
      "firstname": "Demo",
      "lastname": "User",
      "email": "[email protected]",
      "password": "password",
      "confirm": "password",
      "telephone": "1-541-754-3010",
      "customer_group_id": 1,
      "agree": 1,
      "custom_field": {
        "account": {
          "1": " 364545454"
        }
      }
    }

it showing error

    type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in type cast

this may help you:

"https://opencart3-simple.api.opencart-api.com/api/rest/register" -H  "accept: application/json" -H  "X-Oc-Session: 8d63ab88c43d2ebdda1b219a98" -H  "X-Oc-Merchant-Id: 123" -H  "Content-Type: application/json" -d "{  \"firstname\": \"Demo\",  \"lastname\": \"User\",  \"email\": \"[email protected]\",  \"password\": \"password\",  \"confirm\": \"password\",  \"telephone\": \"1-541-754-3010\",  \"customer_group_id\": 1,  \"agree\": 1,  \"custom_field\": {    \"account\": {      \"1\": \" 364545454\"    }  }}"

CodePudding user response:

Encode your data and follow the convention

var response = await _dio.post("$url",
          options: header, data: jsonEncode(data));

CodePudding user response:

Map<Strng,dynamic>` data = 

    {
      "firstname": "Demo",
      "lastname": "User",
      "email": "[email protected]",
      "password": "password",
      "confirm": "password",
      "telephone": "1-541-754-3010",
      "customer_group_id": 1,
      "agree": 1,
      "custom_field": jsonEncode({
        "account": {
          "1": " 364545454"
        }
      })
    }

CodePudding user response:

You can pass like that :

  var data = {}; 

  data["firstname"] = "Demo";
  data["lastname"] = "User";
  data["email"] = "[email protected]";
  data["password"] ="password";

and pass like this

 var response = await _dio.post($url,
           data: jsonEncode(data));
  • Related