YohDono
YohDono

Reputation: 37

Issue with callback


I would like to create a generic dialog but I have some issue with the callback... Here is my code :
The dialog :

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")
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

The widget :
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,
            )
          },
        );
      },
    );
  }

Everytime I "longpress", I got this error message :

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

The product I pass as parameter is not null and the dialog don't show up but the method "deleteSelectedProduct" is triggered.
I can't figure out where I got it wrong? If someone can help, it would be great!
Thank you in advance

Upvotes: 0

Views: 95

Answers (1)

sirwilliam15
sirwilliam15

Reputation: 53

The function is being called before it is passed in, so it will return null and pass that value in. You would need to use deleteSelectedProduct instead of deleteSelectedProduct(product)

Upvotes: 1

Related Questions