英文:
Why do I get this error: _CastError (Null check operator used on a null value)
问题
我有一个类似这样的列:
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'),
]),
然后我得到这个错误:
_CastError (在空值上使用了空值检查运算符)
我知道变量可能为空,这就是我把它们放在空值检查if语句中的原因,但我不知道为什么会出现空值检查错误,以及如何解决它?
Flutter强制我在空变量后面加上!,然后由于这个原因出现了错误!
英文:
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!
答案1
得分: 1
代码中的问题是因为 product.videos 为 null,尽管你已经处理了它为 null 的情况,但你使用了 ! 运算符来告诉 Dart 编译器 product.videos 一定不会为 null。请将 ! 替换为 ?,表示它可能为 null,如果为 null,则会采取预防措施。
将你的代码中的 ! 替换为 ?:
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'),
编辑注释中的解释:
!- 声明编译器值一定不会为null。
var val1 = val2! // val2 一定不会为 null
?- 声明编译器值可以为null。
String? val; // 这里 val 可能为 null
?.- 只有在不为 null 时访问属性。
object?.prop1; // 只有在 object 不为 null 时才访问 prop1
??- 如果值为 null,则使用备用值。
var val1 = val2 ?? val3; // 如果 val2 为 null,将 val3 赋给 val1
英文:
It is because product.videos is null, though you handled the condition if it is null, but you are assuring dart compiler that product.videos can nver be null , by using ! opeartor. Change ! to ? meaning , it may be subjected to being null, and precautions would be taken if it is null.
Change your code by replacing ! to ?:
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'),
Edit for the comment:
>Could you explain what is the difference between ! and ?. and ? and ??
!- Saying the compiler value can never benull.
var val1 = val2! // val2 can never be null
?- Saying the compiler value can benull.
String? val; // Here val can be potentially null
?.- Access only if not null.
object?.prop1; // Here prop1 is accessed only if object is not null
??- Alternate value if the value is null
var val1 = val2 ?? val3; // if val2 is null assign va13 to val1
答案2
得分: 0
看起来你的 product.videos 本身是 null。所以在 if 条件中将 ! 更改为 ?。
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'),
]),
英文:
Looks your product.videos itself null. So change ! to ? in if condition.
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'),
]),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论