【发布时间】: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: '<Unexpected Null Value>' 错误仍然按钮不起作用
并且也尝试了这种方法,但仍然显示相同并且 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