首页 > 解决方案 > 回调问题

问题描述


我想创建一个通用对话框,但回调有一些问题......这是我的代码:
对话框:
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”方法。
我不知道我哪里弄错了?如果有人可以提供帮助,那就太好了!
先感谢您

标签: flutterdartcallback

解决方案


该函数在传入之前被调用,因此它将返回 null 并将该值传入。您需要使用deleteSelectedProduct而不是deleteSelectedProduct(product)


推荐阅读