【问题标题】:flutter ontap error ontap stopped working扑动 ontap 错误 ontap 停止工作
【发布时间】:2021-11-18 20:18:06
【问题描述】:

在 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;' 错误,但按钮仍然不起作用

【问题讨论】:

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


    【解决方案1】:

    尝试将其包装在一个函数中以获得清晰的代码。

    , 替换为 ; 以结束您的命令

    InkWell(
           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);
              }
    }
    

    祝你好运。

    【讨论】:

    • 我收到Another exception was thrown: NoSuchMethodError: '&lt;Unexpected Null Value&gt;' 错误,但按钮仍然不起作用
    • 如果我尝试删除 if 语句并添加 onTap:onTap它正在工作
    猜你喜欢
    • 2021-11-18
    • 2021-11-21
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多