【问题标题】:flutter onTap error onTap stopped working after adding if statement添加 if 语句后,颤振 onTap 错误 onTap 停止工作
【发布时间】:2021-11-18 23:12:36
【问题描述】:

在 ontap 中添加 if 语句后,flutter 按钮停止工作时出现 null 错误,这是我的颤振代码

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 (!isIncrement && quantity > 1) {
          Get.find<ProductController>().setQuantity(false),
        } else if (isIncrement) {
          if (quantity < stock) {
            Get.find<ProductController>().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),
        ),
        alignment: Alignment.center,
        child: Icon(
          isIncrement ? Icons.add : Icons.remove,
          size: 15,
          color: isIncrement
              ? Theme.of(context).cardColor
              : Theme.of(context).disabledColor,
        ),
      ),
    );
  }
}

错误:如果我尝试删除 if 语句并添加 onTap:onTap 它正在工作我得到 Another exception was thrown: NoSuchMethodError: '&lt;Unexpected Null Value&gt;' 错误仍然按钮不起作用 并且也尝试了这种方法,但仍然显示相同并且 onTap 不起作用

       onTap : (){
        if (!isIncrement && quantity > 1) {
          Get.find<ProductController>().setQuantity(false);
        } else if (isIncrement) {
          if (quantity < stock) {
            Get.find<ProductController>().setQuantity(true);
          } else {
            showCustomSnackBar('out_of_stock'.tr);
          }
} ```

【问题讨论】:

  • 你能简单解释一下你想在这里实现什么吗?
  • onTap 功能不工作

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


【解决方案1】:

这是因为onTap 参数需要一个VoidCallback 对象,而您可以通过两种方式声明它:

一次性功能。特定于小部件,其他人无法调用。

onTap : (){
    ... your code ...
  }

或者以这种方式声明一个新函数

void yourFunction(){
    ... your code ...
  }

并在您的按钮中使用该功能

onTap : yourFunction

第二种方式允许你从不同的Widget调用函数

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2021-11-21
    • 2022-08-09
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    相关资源
    最近更新 更多