【问题标题】:Flutter onTap stopped working and null errorFlutter onTap 停止工作并出现空错误
【发布时间】:2021-11-21 13:12:27
【问题描述】:

在 ontap 中添加 if 语句后,在颤振按钮停止工作时出现空错误这是我的颤振代码错误 Another exception was thrown: NoSuchMethodError: '<Unexpected Null Value>'
这是我的按钮代码

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

请帮我解决这个问题而不删除 onTap : onTap 函数

【问题讨论】:

    标签: flutter android-studio flutter-layout flutter-dependencies flutter-web


    【解决方案1】:

    错误源于您提供给 InkwWell 小部件的 onTap 参数的值, onTap 参数需要{void Function()?},而您提供的类型不正确。 您正在做的是使用 Set 类型的参数调用小部件的 onTap,而不是提供函数本身。 解决方案是像这样将小部件的 onTap 直接传递给 InkWell onTap

    InkWell(
          onTap: onTap,
    

    或者如果你想用参数调用 onTap,你可以使用这样的 lambda 函数:

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

    这两个解决方案应该可以工作,但我在这里看到的是,您在调用 onTap 时尝试定义逻辑,这是不正确的。在创建 QuantityButton 类时应定义小部件的 onTap 逻辑,如下所示:

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

    【讨论】:

    • 我如何定义功能现在我都尝试了但没有工作
    • 好的,所以在 Inkwell 小部件中,直接将 onTap 参数赋予 onTap 属性,就像答案中的第一个代码示例一样,在 QuantityButton 实例化中,将答案中的第三个代码简单地添加省略了参数。
    • 抱歉再问一次,你能在代码中显示如何提及它吗?我试过了,但没有用
    • 我在这个错误上得到错误是the default value of an optional parameter must be constant
    • 没有仍然出现意外的空值错误
    猜你喜欢
    • 2021-11-18
    • 2021-11-18
    • 2013-07-02
    • 2015-08-28
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    相关资源
    最近更新 更多