flutter - 在颤动中发送多部分请求中的对象
问题描述
在这里,我试图在多部分请求中发送对象映射,但我的请求作为字符串发送,我需要发送对象映射而不是字符串。但 request .fields 想要我的字符串。
"barberAddress":{
"State":"test",
"City":"test",
"Street": "test",
"location":{
"lat":1.2,
"lang":2.1
}
},
这是我的帖子方法
postFormDataBarber({
@required String endPoint,
File picB,barberName,BarberAddress barberAddress, }) async {
var url = Uri.parse('${BaseUrl.baseUrl}$endPoint');
var request = http.MultipartRequest("POST", url);
request.fields["barberAddress"] = json.encode(barberAddress);// {"State":"aaa","City":"vvv","Street":"eeee","location":{"lat":null,"lang":null}}
request.files.add(picB);
final data = await request.send();
return data;
}
这是我的模型,我想将此模型映射对象添加到 Mango 数据库
class BarberAddress {
BarberAddress({
@required this.state,
@required this.city,
@required this.street,
this.location,
});
String state;
String city;
String street;
BarberLocation location;
factory BarberAddress.fromJson(Map<String, dynamic> json) =>
BarberAddress(
state: json["State"],
city: json["City"],
street: json["Street"],
location: BarberLocation.fromJson(json["location"]),
);
class BarberLocation {
BarberLocation({
@required this.lat,
@required this.lang,
});
double lat;
double lang;
factory BarberLocation.fromJson(Map<String, dynamic> json) =>
BarberLocation(
lat: json["lat"].toDouble(),
lang: json["lang"].toDouble(),
);
Map<String, dynamic> toJson() => {
"lat": lat,
"lang": lang,
};
}
解决方案
推荐阅读
- python - 在 pymssql 中调用 SQL SERVER 的存储过程时传递可选参数
- filter - 在过滤器数据阶段转义单引号或 aphosthrope
- xpath - 无法使用 xpath following-sibling 获取元素
- powershell - 验证用户输入(路径)
- javascript - HTML表格中的隐藏行导致问题
- install4j - Java 可执行 Jar 作为服务
- google-chrome - window.customElements.define 为 null - 无法创建 Shadow DOM(在 Chrome 扩展中)
- node.js - . 你如何防止/修复回调地狱?
- sql-server - 为什么更新行时 VARCHAR 比 CHAR 慢?
- java - java.lang.NoSuchMethodError: org.json.JSONObject.entrySet()Ljava/util/Set