回调问题

Issue with callback


我想创建一个通用对话框,但回调有一些问题...这是我的代码:
对话框:
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,
            )
          },
        );
      },
    );
  }

每次我“长按”时,都会收到此错误消息:

The following _TypeError was thrown while handling a gesture: type 'Null' is not a subtype of type '(Product) => dynamic'

我作为参数传递的产品不为空,对话框没有显示,但触发了方法“deleteSelectedProduct”。
我无法弄清楚我哪里错了? 如果有人能帮忙,那就太好了!
提前谢谢你

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