When I try to get product list in a controller for a ajax module like this :
echo $this->layoutFactory->create()
->createBlock('\Magento\Catalog\Block\Product\ListProduct')
->setCollection($productCollection)
->setTemplate('Magento_Catalog::product/list.phtml')
->toHtml();
I have this error : Fatal error: Uncaught Error:
Call to a member function setData() on bool in /var/www/magento2/vendor/magento/module-catalog/Block/Product/ListProduct.php:419 Stack trace:
#0 /var/www/magento2/vendor/magento/module-catalog/Block/Product/ListProduct.php(391): Magento\Catalog\Block\Product\ListProduct->getPriceRender()
#1 /var/www/magento2/generated/code/Magento/Catalog/Block/Product/ListProduct/Interceptor.php(180): Magento\Catalog\Block\Product\ListProduct->getProductPrice(Object(Magento\Catalog\Model\Product\Interceptor))
#2 /var/www/magento2/app/design/frontend/Vital/vital/Magento_Catalog/templates/product/list.phtml(121): Magento\Catalog\Block\Product\ListProduct\Interceptor->getProductPrice(Object(Magento\Catalog\Model\Product\Interceptor))
#3 /var/www/magento2/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/var/www/magent...')
#4 /var/www/magento2/vendor/magento/module-developer/Model/TemplateEngine/Decorator/DebugHints.php(46): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catal in /var/www/magento2/vendor/magento/module-catalog/Block/Product/ListProduct.php on line 419
Does anyone know where the problem is coming from and how to solve it?
Thanks