我的无状态小部件触发了一条我无法分配的错误消息



我经常在这里找到非常快速、非常能干的帮助。希望你能再次帮助我。我收到一条错误消息已经三天了,我无法解决。我在一个小视频中向你解释了这个问题。视频链接为:"https://drive.google.com/file/d/1T9uOnEaNp5W6_kcO6eV9o64Fp3sRkB3f/view?usp=sharing">

我真的希望你看到我没有看到的错误!

这是Foodcard的代码,根据Flutter的说法,这应该会导致错误:

The method '-' was called on null.
Receiver: null
Tried calling: -(30.0)
The relevant error-causing widget was: 
FoodCard file:///C:/Users/stefa/AndroidStudioProjects/cronum_app_web%20-%20Kopie/lib/Components/ProviderComponents.dart:298:9

FoodCard:

class FoodCard extends StatelessWidget {
FoodCard({
@required this.description,
@required this.imagePath,
@required this.price,
@required this.foodName,
@required this.foodItem,
@required this.increaseCallback,
@required this.decreaseCallback,
this.count = 0,
});
final double radius = 40;
static double listViewHeight;
final double margin = 15;
final double containerHeight = 130;
final int count;
final String imagePath;
final String foodName;
final String price;
final String description;
final FoodItem foodItem;
final Function increaseCallback;
final Function decreaseCallback;
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 10, vertical: margin),
width: 220,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(radius),
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.topRight,
colors: ([
gradientColor1.withOpacity(opacityOfHeader),
primaryColor.withOpacity(opacityOfHeader),
gradientColor2.withOpacity(opacityOfHeader),
]),
),
boxShadow: [
BoxShadow(
blurRadius: 8,
color: Colors.black.withOpacity(0.3),
offset: Offset(5, 5),
),
],
color: Colors.white,
),
child: Column(
children: <Widget>[
Stack(
alignment: Alignment.center,
children: <Widget>[
Column(
children: <Widget>[
SizedBox(
height: listViewHeight - margin * 2 - containerHeight,
),
Container(
height: containerHeight,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(radius),
topRight: Radius.circular(radius),
bottomLeft: Radius.circular(radius),
bottomRight: Radius.circular(radius),
),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
offset: Offset(0, -5),
blurRadius: 8)
],
color: Colors.white),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 40),
child: Center(
child: Text(
foodName,
style: TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.w900,
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 20, vertical: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
foodItem: foodItem,
increaseCount: increaseCallback,
decreaseCount: decreaseCallback,
),
),
);
},
child: Container(
height: 30,
width: 80,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
offset: Offset(5, 5),
color: Colors.black.withOpacity(0.3),
blurRadius: 8)
],
color: buttonColor,
borderRadius: BorderRadius.circular(25),
),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 7, vertical: 5),
child: Center(
child: Text(
'Details',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
Container(
color: Colors.grey.withOpacity(0.5),
height: 25,
width: 1,
),
Container(
height: 30,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
color: buttonColor,
boxShadow: [
BoxShadow(
blurRadius: 8,
color: Colors.black.withOpacity(0.3),
offset: Offset(5, 5),
),
],
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: <Widget>[
InkWell(
onTap: decreaseCallback,
child: Container(
height: 22,
width: 22,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(5),
color: buttonColor,
),
child: Center(
child: Icon(
Icons.remove,
color: Colors.white,
size: 22,
),
),
),
),
Text(
count.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w900,
),
),
InkWell(
onTap: increaseCallback,
child: Container(
height: 22,
width: 22,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(15),
color: Colors.white,
),
child: Center(
child: Icon(
Icons.add,
color: buttonColor,
size: 22,
),
),
),
),
],
),
),
],
),
),
],
),
),
],
),
Positioned(
top: 30,
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
foodItem: foodItem,
increaseCount: increaseCallback,
decreaseCount: decreaseCallback,
),
),
);
},
child: Image(
height: listViewHeight / 2,
width: listViewHeight / 2,
image: AssetImage(imagePath),
),
),
),
],
)
],
),
);
}

如果不是因为FoodCard,那么创建FoodCard列表的函数也可能是问题所在。我对这个错误消息感到惊讶,因为它是如此的不具体。在我自动创建列表之前,一切都非常顺利。


List<Widget> buildEntdeckenCards() {
List<Widget> foodCardList = [];
for (FoodItem foodItem in top10) {
print(foodItem.foodName);
foodCardList.add(
FoodCard(
description: foodItem.description,
foodName: foodItem.foodName,
foodItem: foodItem,
price: foodItem.price,
imagePath: foodItem.imagePath,
decreaseCallback: () {
decreaseCount(foodItem);
},
increaseCallback: () {
increaseCount(foodItem);
},
),
);
}
return foodCardList;
}

我认为listViewHeight变量为null,您正试图在表达式中使用它

SizedBox(height: listViewHeight - margin * 2 - containerHeight,),

我认为这就是您看到以下错误The method '-' was called on null的原因。在使用listViewHeight之前为其指定一些值。

相关内容

  • 没有找到相关文章

最新更新