loadedPorduct
is not available outside the if(productId!=null)
.
Place this variable out of it, inside the build context.
late final loadedProduct ; final productId = ModalRoute.of(context)?.settings.arguments as String?;if (productId != null) { loadedProduct = Provider.of<Products>(context) .items .firstWhere((prod) => prod.id == productId); }
And use case will be
Text(loadedProduct?.title?? "default TextOn null case"),