使用嵌套JSON作为数据+ BLoC模式的Flutter Post请求

问题描述

我尝试使用BLoC模式在发布请求中传递JSON。

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'
});

我找到了使用 jsonEncode 传递JSON的所有解决方案,但是在Flutter的Post Request中没有找到传递嵌套JSON的解决方案。

这是我要传递的JSON:

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

有人可以告诉我如何发送嵌套的JSON来发布Flutter的请求吗?

解决方法

请在下面尝试

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));
,

将JSON转换为模型然后使用它总是一个好习惯。

TitleView

我已经将JSON转换为模型,现在您可以在任何需要的地方使用它。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...