我有这个要发送到服务器的键值对:
String brand = "sony";
double price_val = 0.01,
ram_val = 0.01,
internal_val = 0.01,
kamera_val = 0.01;
var arguments = {
"brand": brand,
"price": price_val,
"ram": ram_val,
"internal": internal_val,
"kamera": kamera_val
};
但是我得到了这个错误:
Error : type 'double' is not a subtype of type 'String' in type cast
如果我将其更改为.toString()
,则服务器将给出另一个错误,因为值必须为double
。
我该如何准确发送这些值?
编辑
它似乎在http请求中给出了错误:
http.post(url,
body: arguments,
headers: {"Content-Type": "application/json"}).then((onValue) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResultPage(
onValue.body,
)));
});
答案 0 :(得分:1)
如果您在请求标头中将application/json
设置为内容类型,则需要使用jsonEncode
将请求正文编码为json
这是示例
http.post(url,
body: jsonEncode(arguments),
headers: {"Content-Type": "application/json"}).then((onValue) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResultPage(
onValue.body,
)));
});
答案 1 :(得分:0)
明确定义参数类型
final arguments = <String, Object>{
"brand": brand,
"price": price_val,
"ram": ram_val,
"internal": internal_val,
"kamera": kamera_val
};
答案 2 :(得分:0)
您可以使用动态关键字:
final arguments = <String, dynamic>{
"brand": brand,
"price": price_val,
"ram": ram_val,
"internal": internal_val,
"kamera": kamera_val
};