参数类型' productModel?'可以将参数类型分配给ProductModel'

发布于 2025-01-31 14:33:21 字数 767 浏览 1 评论 0原文

我正在制作一个Midel,以将产品图像,价格和名称上传到Firebase,然后我遇到此错误(参数类型'ProductModel?'无法分配到参数类型'ProductModel'。)。

    class ProductProvider with ChangeNotifier {
  List<ProductModel> pizzaProductList = [];
  ProductModel? productModel;

  fatchPizzaproductData() async {
   // List<ProductModel> newList = [];

    QuerySnapshot value =
        await FirebaseFirestore.instance.collection("PizzaProducts").get();

     pizzaProductList = value.docs.map((element) {
      return ProductModel(
        productImage: element.get("productImage"),
        productName: element.get("productName"),
        productPrice: element.get("productPrice"),
      );
    }).toList();
    
  }

  get getPizzaproductDataList {
    return pizzaProductList;
  }
}

I'm making a midel to upload product image,price and name to firebase then i face this error (The argument type 'ProductModel?' can't be assigned to the parameter type 'ProductModel'.)

    class ProductProvider with ChangeNotifier {
  List<ProductModel> pizzaProductList = [];
  ProductModel? productModel;

  fatchPizzaproductData() async {
   // List<ProductModel> newList = [];

    QuerySnapshot value =
        await FirebaseFirestore.instance.collection("PizzaProducts").get();

     pizzaProductList = value.docs.map((element) {
      return ProductModel(
        productImage: element.get("productImage"),
        productName: element.get("productName"),
        productPrice: element.get("productPrice"),
      );
    }).toList();
    
  }

  get getPizzaproductDataList {
    return pizzaProductList;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

属性 2025-02-07 14:33:21

问题是productModel是一种不可用的类型,而pizzaproduct不可删除的 productmodel s的列表。

而不是在类上存储属性productModel,而是考虑从value.docs pizzaproduct 直接映射,并删除存储模型的中间步骤在productModel中:

class ProductProvider with ChangeNotifier {
  List<ProductModel> pizzaProduct = [];

  Future<void> fetchPizzaProductData() async {
    QuerySnapshot value =
        await FirebaseFirestore.instance.collection("PizzaProducts").get();

    pizzaProduct = value.docs.map((element) {
      return ProductModel(
        productImage: element.get("productImage"),
        productName: element.get("productName"),
        productPrice: element.get("productPrice"),
      );
    }).toList();

    // Since this is a ChangeNotifier, I'm assuming you might want to
    // notify listeners when `pizzaProduct` changes. Disregard this line
    // if that's not the case.
    notifyListeners();
  }
}

The problem is that productModel is a nullable type, whereas pizzaProduct is a List of non-nullable ProductModels.

Instead of storing a property productModel on your class, consider mapping directly from value.docs to pizzaProduct, and removing the intermediate step of storing the model in productModel:

class ProductProvider with ChangeNotifier {
  List<ProductModel> pizzaProduct = [];

  Future<void> fetchPizzaProductData() async {
    QuerySnapshot value =
        await FirebaseFirestore.instance.collection("PizzaProducts").get();

    pizzaProduct = value.docs.map((element) {
      return ProductModel(
        productImage: element.get("productImage"),
        productName: element.get("productName"),
        productPrice: element.get("productPrice"),
      );
    }).toList();

    // Since this is a ChangeNotifier, I'm assuming you might want to
    // notify listeners when `pizzaProduct` changes. Disregard this line
    // if that's not the case.
    notifyListeners();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文