我正在收到一个被称为空颤音中的错误错误。在尝试访问列表的长度时
当我调用getCartQuantity()方法时,请呼叫getter长度。关于手推车无效的东西。我不知道问题是什么。
这是实际错误消息! Nosuchmethoderror:null称为“长度”。 接收器:null 尝试调用:长度
import 'package:butcherbox/models/productsModel.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class StoreLogic extends ChangeNotifier {
List<ProductsModel> _products;
List<ProductsModel> _cart;
ProductsModel _activeProduct = null;
StoreLogic() {
_products = [
ProductsModel(
id: 001,
imageText: 'images/biggiecombo.jpg',
name: 'ButcherBox Mini Combo Pack',
category: 'Beef and Chicken',
price: 500,
quantity: 0,
),
ProductsModel(
id: 002,
imageText: 'images/thecombopack.jpg',
name: 'ButcherBox Biggie Combo',
category: 'Beef and Chicken',
price: 950,
quantity: 0,
),
ProductsModel(
id: 003,
imageText: 'images/regular1kg.jpg',
name: 'ButcherBox Regular 1kg',
category: 'Beef',
price: 1800,
quantity: 0,
),
];
notifyListeners();
}
List<ProductsModel> get products => _products;
List<ProductsModel> get cart => _cart;
ProductsModel get activeProduct => _activeProduct;
setActiveProduct(ProductsModel p) {
_activeProduct = p;
}
addOneItemToCart(ProductsModel p) {
ProductsModel found =
_cart.firstWhere((a) => a.id == p.id, orElse: () => null);
if (found != null) {
found.quantity += 1;
} else {
_cart.add(p);
}
notifyListeners();
}
getCartQuantity() {
int totalItems = 0;
for (int i = 0; i < cart.length; i++) {
totalItems += cart[i].quantity;
}
return totalItems;
}
}
cart.length返回错误
Getting a getter length called on null error when I call the getCartQuantity() method. Something about the cart being null. I do not know what the problem is.
This is the actual ERROR MESSAGE!
NoSuchMethodError: The getter 'length' was called on null.
Receiver: null
tried calling: length
import 'package:butcherbox/models/productsModel.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class StoreLogic extends ChangeNotifier {
List<ProductsModel> _products;
List<ProductsModel> _cart;
ProductsModel _activeProduct = null;
StoreLogic() {
_products = [
ProductsModel(
id: 001,
imageText: 'images/biggiecombo.jpg',
name: 'ButcherBox Mini Combo Pack',
category: 'Beef and Chicken',
price: 500,
quantity: 0,
),
ProductsModel(
id: 002,
imageText: 'images/thecombopack.jpg',
name: 'ButcherBox Biggie Combo',
category: 'Beef and Chicken',
price: 950,
quantity: 0,
),
ProductsModel(
id: 003,
imageText: 'images/regular1kg.jpg',
name: 'ButcherBox Regular 1kg',
category: 'Beef',
price: 1800,
quantity: 0,
),
];
notifyListeners();
}
List<ProductsModel> get products => _products;
List<ProductsModel> get cart => _cart;
ProductsModel get activeProduct => _activeProduct;
setActiveProduct(ProductsModel p) {
_activeProduct = p;
}
addOneItemToCart(ProductsModel p) {
ProductsModel found =
_cart.firstWhere((a) => a.id == p.id, orElse: () => null);
if (found != null) {
found.quantity += 1;
} else {
_cart.add(p);
}
notifyListeners();
}
getCartQuantity() {
int totalItems = 0;
for (int i = 0; i < cart.length; i++) {
totalItems += cart[i].quantity;
}
return totalItems;
}
}
The cart.length returns the error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在执行操作之前,请检查您的购物车是否为null。
但总的来说,您似乎没有使用Null-Safety,我强烈建议您这样做。
Check if your cart is null before performing an operation on it.
But in general, it appears you are not using null-safety, which I strongly advise you to do.