我正在收到一个被称为空颤音中的错误错误。在尝试访问列表的长度时

发布于 2025-02-06 11:01:34 字数 1823 浏览 5 评论 0原文

当我调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2025-02-13 11:01:35

在执行操作之前,请检查您的购物车是否为null。

 getCartQuantity() {
    int totalItems = 0;
    if(cart == null) return totalItems; //add this line.
    for (int i = 0; i < cart.length; i++) {
      totalItems += cart[i].quantity;
    }
    return totalItems;
  }

但总的来说,您似乎没有使用Null-Safety,我强烈建议您这样做。

Check if your cart is null before performing an operation on it.

 getCartQuantity() {
    int totalItems = 0;
    if(cart == null) return totalItems; //add this line.
    for (int i = 0; i < cart.length; i++) {
      totalItems += cart[i].quantity;
    }
    return totalItems;
  }

But in general, it appears you are not using null-safety, which I strongly advise you to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文