Home > Software design >  The argument type 'ProductModel?' can't be assigned to the parameter type 'Produ
The argument type 'ProductModel?' can't be assigned to the parameter type 'Produ

Time:05-26

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;
  }
}

CodePudding user response:

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();
  }
}
  • Related