flutter - 回调问题
问题描述
我想创建一个通用对话框,但回调有一些问题......这是我的代码:
对话框:
class ConfirmDialog {
static Future show<T>(BuildContext context, String message,
void Function(T) onConfirm, VoidCallback? onRefuse) {
return showDialog(
context: context,
builder: (context) {
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.grey[300],
),
child: Container(
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
width: 400,
height: 200,
child: Card(
child: Column(
children: [
Text(
message,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 30),
),
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Container(
height: 70,
padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
child: Image.asset("assets/images/confirm.png"),
),
onTap: () => {
onConfirm,
print("accept"),
Navigator.pop(context),
}),
GestureDetector(
child: Container(
height: 70,
padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
child: Image.asset("assets/images/cancel.png"),
),
onTap: () => {
if (onRefuse != null)
{
onRefuse,
}
else
{
Navigator.pop(context),
}
print("refuse")
},
),
],
),
),
],
),
),
),
);
},
);
}
小部件:
Widget listView(List<Product> products) {
return ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
Product product = products[index];
return GestureDetector(
child: Container(
height: 150,
child: Card(
child: Row(
children: [
Container(
alignment: Alignment.center,
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
width: 150,
height: 150,
child: Image.network(product.small),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
product.name,
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: false,
style: TextStyle(
fontSize: 30,
),
),
Text(
product.getBrand(),
style: TextStyle(
color: Colors.grey,
fontSize: 25,
fontFamily: "Antonio",
),
)
],
),
)
],
),
),
),
onTap: () => {},
onLongPress: () => {
ConfirmDialog.show<Product>(
context,
"Are you sure you want to delete\n${product.name} ?",
deleteSelectedProduct(product),
null,
)
},
);
},
);
}
每次我“长按”时,都会收到以下错误消息:
处理手势时引发以下 _TypeError:类型“Null”不是“(产品)=> 动态”类型的子类型
我作为参数传递的产品不为空,并且对话框不显示,但触发了“deleteSelectedProduct”方法。
我不知道我哪里弄错了?如果有人可以提供帮助,那就太好了!
先感谢您
解决方案
该函数在传入之前被调用,因此它将返回 null 并将该值传入。您需要使用deleteSelectedProduct
而不是deleteSelectedProduct(product)
推荐阅读
- python - Flask 单元测试在数据库中创建额外的条目
- c++ - 如何为未知数量的输入创建新结构
- javascript - 使用 Javascript 的 Photoshop 字符样式复制
- rxjs - RxJS:在外部 observable 完成之前分批接收 10 个?
- python - Heroku 上的 Flask 站点不断用新的 PID 引导工作人员
- json - 尝试向我的 Java Web 应用程序发出请求时出错 java.lang.IllegalStateException:在提交响应后无法调用 sendError()
- linux - Traefik 和 Deluge 返回 No such Resource
- aurelia - 使用 aurelia-slickgrid 从上下文菜单回调访问视图模型
- python-3.x - 在python中对分区列表进行就地词法排序
- python - PyAudio callback function called only once