您好,我在这里遇到了一些小问题,我只是在点击“添加到购物车”按钮,然后快速按下手机上的“后退”按钮时发现了这一点。
通过http.dart在flutter上调用post API时,出现以下错误: “未处理的异常:在收到完整的标头之前,连接已关闭”
RaisedButton(
child: Text("add to cart"),
onPressed: () async{
await funcAddToCart(widget.buCode,widget.tenantCode,widget.prodId,itemCount,loadItemData[index]['prodPrice']);
},
)
//my function call after button click
Future funcAddToCart(buCode,tenantCode,prodId,itemCount,price) async{
int customerId = 12;
Map data = {
'customerId':customerId,
'buCode':buCode,
'tenantCode':tenantCode,
'prodId':prodId,
'itemCount':itemCount,
'price':price
};
//encode Map to JSON
var body = json.encode(data);
await http.post('http://172.0.0.1:3000/addToCart',
headers: {"Content-Type": "application/json"},
body: body
);
}
// my node js route that receive the post call
router.post('/addToCart', function (req, res) {
var customerId = req.body.customerId;
var buCode = req.body.buCode;
var tenantCode = req.body.tenantCode;
var prodId = req.body.prodId;
var itemCount = req.body.itemCount;
var price = req.body.price;
db.query("INSERT INTO app_cart (customerId,buId,tenantId,productId,quantity,price,createdAt,updatedAt) VALUES (? ,? ,? ,? ,? ,? ,? ,?)",['12',buCode,tenantCode,prodId,itemCount,price,new Date(), new Date()], function (err, result, fields) {
if (err) throw err;
});
})