I have a column like this:
Column(children: [
product.videos![product.videoIndex].videoUrl != null &&
product.videos![product.videoIndex].videoUrl != ""
? VideoPlayerWidget(
videoAddress: product.videos![product.videoIndex].videoUrl)
: Image.asset('assets/images/video-placeholder.jpg'),
]),
And I get this error:
_CastError (Null check operator used on a null value)
I know that the variables may be null and that's the reason I put them within a null check if statement, but I don't know why do I get the null check error and how can I pass that?
The Flutter forced me to put ! after null variables, then it gives me error because of that!
It is because
product.videosisnull, though you handled the condition if it isnull, but you are assuringdartcompiler thatproduct.videoscan nver benull, by using!opeartor. Change!to?meaning , it may be subjected to beingnull, and precautions would be taken if it isnull.Change your code by replacing
!to?:Edit for the comment:
!- Saying the compiler value can never benull.?- Saying the compiler value can benull.?.- Access only if not null.??- Alternate value if the value is null