Search code examples
jsonflutterblocjsondecoderjsonencoder

Flutter Post Request with Nested JSON as Data + BLoC pattern


I have tried to pass JSON in Post Request using BLoC Pattern.

jsonEncode(<String, String>{
  'MobileNo': _emailController.value,
  'Password': _passwordController.value,
  'IPAddress': '192.168.0.1',
  'Latitude' : '23.04503',
  'Longitude': '72.55919',
  "wauid" : 'd4KY17YySLC8-ROzs1RoJN:APA91bHMVz-4tw7cRIrEmBU2wHr_YW1RgV5HQfcfQp1YQwkamDPUimiPrfisezPuOgghJgHepXixsRh1Rl_eu75E9qss4RzxM6bGIgQdSo-S9TvynJsfdztz67LiaWbC9fs4xlCZnFQc'
});

I have found all the solutions to pass JSON using jsonEncode but I didn't found any solution to pass Nested JSON in Post Request of Flutter.

Here is the JSON which I am passing:

{
    "userMaster": {
        "MobileNo": "8800112233",
        "Password": "564452",
        "Latitude": 23.04503,
        "Longitude": 72.55919,
        "IPAddress": "5f7f51e7-09f5-4cf2-87f3-ca5760f1ed57",
        "wauid": "12312"
    },
    "loginInfo" : {
        "UserID":0
    }
}

Can anyone please tell me How to send nested JSON to post the request of Flutter?


Solution

  • Please try below

    Map<String, dynamic> payload = {
     "userMaster": {
            "MobileNo": "8800112233",
            "Password": "564452",
            "Latitude": 23.04503,
            "Longitude": 72.55919,
            "IPAddress": "5f7f51e7-09f5-4cf2-87f3-ca5760f1ed57",
            "wauid": "12312"
        },
        "loginInfo" : {
            "UserID":0
        }
    }    
    
    
    Response response = await http.post(<URL>,body: json.encode(payload));