Flutter onTap stopped working and null error

Viewed 533

i'm getting null error in flutter button stopped working after added if statement inside the ontap here is my flutter code error Another exception was thrown: NoSuchMethodError: '<Unexpected Null Value>'
here is my button code

import 'package:efood_multivendor/controller/product_controller.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_instance/src/extension_instance.dart';
import 'package:get/get_utils/src/extensions/internacionalization.dart';

import 'custom_snackbar.dart';

class QuantityButton extends StatelessWidget {
  final bool isIncrement;
  final Function onTap;
  final int quantity;
  final int stock;
  QuantityButton(
      {@required this.isIncrement,
      @required this.onTap,
      @required this.quantity,
      @required this.stock});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap({
      if(quantity < stock) {
      Get.find()<ProductController>(context, listen: false).setQuantity(true);
      } else {
      showCustomSnackBar('out_of_stock'.tr);
      }

      }),
      child: Container(
        height: 22,
        width: 22,
        margin: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_SMALL),
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(
              width: 1,
              color: isIncrement
                  ? Theme.of(context).primaryColor
                  : Theme.of(context).disabledColor),
          color: isIncrement
              ? Theme.of(context).primaryColor
              : Theme.of(context).disabledColor.withOpacity(0.2),
       
        ),
      ),
    );
  }
}

please help me o solve this problem without removing onTap : onTap function

1 Answers

The error is originating from the value you are providing to the onTap parameter of the InkwWell widget, the onTap parameter is expecting a {void Function()?} and what you are providing is not the right type. What you are doing is calling the onTap of the widget with a parameter of type Set, instead of providing the function itself. The solution would be either to pass widget's onTap directly to the InkWell onTap like this

InkWell(
      onTap: onTap,

or if you wanna call the onTap with parameters you can use a lambda function like this :

InkWell(
      onTap: () => onTap(param1: value1, param2: value2),

These two solutions should work but what I see here, is that you are trying to define a logic when calling the onTap, which is not correct. the widget's onTap logic should be defined when creating the QuantityButton class like the following :

QuantityButton(
  onTap : () {
    if(quantity < stock) {
      Get.find()<ProductController>(context, listen: false).setQuantity(true);
     } else {
      showCustomSnackBar('out_of_stock'.tr);
      }
  }, rest of the parameters...);
Related