You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter自定义泛型确认对话框回调类型不匹配错误求助

问题原因

  1. 回调参数传值错误
    你在调用ConfirmDialog.show时直接执行了deleteSelectedProduct(product),将方法返回的void(即Null)传给了要求Function(Product)类型的onConfirm参数,因此直接触发了删除逻辑,同时触发类型不匹配的报错。
  2. 对话框内部回调写法错误
    Dart 中不支持() => { 语句1, 语句2 }的JS风格箭头函数写法,该写法实际会返回一个Set集合,内部的语句不会按预期执行;同时你仅写了onConfirm、onRefuse变量名,没有加括号调用函数,就算参数传对了也不会触发回调逻辑。
  3. 泛型参数未传递
    你定义的泛型回调Function(T)没有对应的传值入口,确认时无法将泛型参数传递给回调。

修正方案

1. 修正ConfirmDialog实现

import 'package:flutter/material.dart';

class ConfirmDialog {
  static Future show<T>(
    BuildContext context,
    String message,
    void Function(T) onConfirm,
    VoidCallback? onRefuse,
    T data, // 新增泛型参数,用于传递给确认回调
  ) {
    return showDialog(
      context: context,
      builder: (context) {
        return Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: Colors.grey[300],
          ),
          child: Container(
            padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
            width: 400,
            height: 200,
            child: Card(
              child: Column(
                children: [
                  Text(
                    message,
                    textAlign: TextAlign.center,
                    style: const TextStyle(fontSize: 30),
                  ),
                  Container(
                    margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        GestureDetector(
                          child: Container(
                            height: 70,
                            padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
                            child: Image.asset("assets/images/confirm.png"),
                          ),
                          onTap: () {
                            onConfirm(data);
                            print("accept");
                            Navigator.pop(context);
                          },
                        ),
                        GestureDetector(
                          child: Container(
                            height: 70,
                            padding: const EdgeInsets.fromLTRB(20, 0, 0, 0),
                            child: Image.asset("assets/images/cancel.png"),
                          ),
                          onTap: () {
                            if (onRefuse != null) {
                              onRefuse();
                            }
                            Navigator.pop(context);
                            print("refuse");
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}

2. 修正对话框调用代码

onLongPress: () => ConfirmDialog.show<Product>(
  context,
  "Are you sure you want to delete ${product.name} ?",
  // 传入函数引用,不要直接执行方法
  (Product p) => deleteSelectedProduct(p),
  null,
  // 传递要用于回调的product参数
  data: product,
),

补充说明

如果不需要用到泛型的动态传参能力,也可以直接把onConfirm的类型改为VoidCallback,调用时直接传() => deleteSelectedProduct(product)即可,不需要额外加data参数。

内容的提问来源于stack exchange,提问作者YohDono

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 12:00:06