json - 在颤振中将请求复杂的json发布到api
问题描述
我有这个 json 我想发布到一个 api 但我不确定它是如何完成的..这是请求的样子:
{
"products": [
{"id":1, "qty": 1},{"id":2, "qty": 1}
],
"vendor_id": 1,
"notes": " ",
"address": ""
}
这是我用来映射请求的请求类:
class Order{
int vendor_id;
String address ,notes ;
List<OrderProduct> products;
Order({this.products , this.address , this.notes , this.vendor_id});
Map<String, dynamic> toMap() {
return {
'vendor_id': vendor_id,
'address': address,
'notes': notes,
'products': products,
};
}
}
class OrderProduct{
int id , qty ;
OrderProduct({this.id , this.qty});
Map<String, dynamic> toMap() {
return {
'id': id,
'qty': qty,
};
}
}
我错过了什么?
解决方案
就这样吧json.encode(myArr)
,例如
Map<String, String> params = {
"mobile": userNameController.text,
"password": passwordController.text,
"deviceid": '${fcmToken}',
};
Future<String> postRequest(var url, {var postParams}) async {
return http
.post(url, body: json.encode(postParams))// do json encoding here
.then((http.Response response) {
final int statusCode = response.statusCode;
print("postParams " + json.encode(postParams));
print("statusCode " + '${statusCode} ${response.body}');
if (statusCode < 200 || statusCode > 400 || json == null) {
throw new Exception("Error while fetching data");
}
print(response.request);
return response.body;
});
}
推荐阅读
- python - 将存储为变量的 unicode 转换为纯文本(例如:“\u0044”到“D”)
- ios - 为什么 swiftUI 会给我正确代码的错误?
- python - 在 Chrome 中的 Jupyter Notebook 中打印 pdf 图像
- reactjs - 在 API 漏洞风险中发送 HTML 内容
- mysql - 使用子查询优化 MySQL SELECT 查询
- spring - 尝试在本地使用 H2 文件进行存储时忽略 Spring 数据源配置
- circleci - 如何在circleci上锁定构建
- specflow - 使用自定义输出路径时无法使用来自外部程序集的 StepDefinitions
- java - AdoptOpenJDK jdk-slim vs jre的目的?
- ios - 验证期间遇到未知错误(无法找到目标 `MyDemo` 的 `contents` 的其他源引用。)