【问题标题】:Flutter Post Request with Nested JSON as Data + BLoC pattern使用嵌套 JSON 作为数据 + BLoC 模式的 Flutter Post 请求
【发布时间】:2021-02-22 06:53:57
【问题描述】:

我尝试使用 BLoC 模式在 Post Request 中传递 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 的请求?

【问题讨论】:

    标签: json flutter bloc jsondecoder jsonencoder


    【解决方案1】:

    请在下面试试

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

    【讨论】:

      【解决方案2】:

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

      class UserDetails {
        UserMaster userMaster;
        LoginInfo loginInfo;
      
        UserDetails({this.userMaster, this.loginInfo});
      
        UserDetails.fromJson(Map<String, dynamic> json) {
          userMaster = json['userMaster'] != null
              ? new UserMaster.fromJson(json['userMaster'])
              : null;
          loginInfo = json['loginInfo'] != null
              ? new LoginInfo.fromJson(json['loginInfo'])
              : null;
        }
      
        Map<String, dynamic> toJson() {
          final Map<String, dynamic> data = new Map<String, dynamic>();
          if (this.userMaster != null) {
            data['userMaster'] = this.userMaster.toJson();
          }
          if (this.loginInfo != null) {
            data['loginInfo'] = this.loginInfo.toJson();
          }
          return data;
        }
      }
      
      class UserMaster {
        String mobileNo;
        String password;
        double latitude;
        double longitude;
        String iPAddress;
        String wauid;
      
        UserMaster(
            {this.mobileNo,
            this.password,
            this.latitude,
            this.longitude,
            this.iPAddress,
            this.wauid});
      
        UserMaster.fromJson(Map<String, dynamic> json) {
          mobileNo = json['MobileNo'];
          password = json['Password'];
          latitude = json['Latitude'];
          longitude = json['Longitude'];
          iPAddress = json['IPAddress'];
          wauid = json['wauid'];
        }
      
        Map<String, dynamic> toJson() {
          final Map<String, dynamic> data = new Map<String, dynamic>();
          data['MobileNo'] = this.mobileNo;
          data['Password'] = this.password;
          data['Latitude'] = this.latitude;
          data['Longitude'] = this.longitude;
          data['IPAddress'] = this.iPAddress;
          data['wauid'] = this.wauid;
          return data;
        }
      }
      
      class LoginInfo {
        int userID;
      
        LoginInfo({this.userID});
      
        LoginInfo.fromJson(Map<String, dynamic> json) {
          userID = json['UserID'];
        }
      
        Map<String, dynamic> toJson() {
          final Map<String, dynamic> data = new Map<String, dynamic>();
          data['UserID'] = this.userID;
          return data;
        }
      }
      

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

      【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2020-05-03
      • 2020-07-29
      相关资源
      最近更新 更多