我想将参数从我的对话框(Void 函数)传递到另一个 void 函数但出现错误
can't be assigned to the parameter type () void flutter
setState 也不起作用。
请在此处查看我的代码:
第一个函数
void _quantity(BuildContext context, productId,quantity){
setState(() {
productId = productId;
quantity = quantity;
_quantityController.text = '$quantity';
});
var alert = new AlertDialog(
actions: <Widget>[
FlatButton(
child: Text("Save"),
onPressed: _addtoCart(context, productId)
)
],
);
showDialog(context: context,builder: (context) => alert);
}
第二个功能:
void _addtoCart(BuildContext context, productId) {
print("Quantity: $quantity");
print("productId: $productId");
print("data: $data");
}
请在此处查看屏幕截图
最佳答案
改变
onPressed: _addtoCart(context, productId)
到
onPressed: () => _addtoCart(context, productId)
传递一个函数,而不是函数调用的结果(_addtoCart()
的返回值,它返回 void
并产生错误。
如果 _addtoCart
不带任何参数,您可以使用较短的形式
onPressed: _addtoCart
但是如果你添加 ()
函数被调用并且返回值被传递而不是使用 () =>
你可以让它再次成为一个函数引用或者在这个关闭的情况。
关于parameters - 将变量传递给 flutter 中的void函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058756/