Why do I get this error: _CastError (Null check operator used on a null value)

huangapple go评论207阅读模式
英文:

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.videosnull,尽管你已经处理了它为 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'),

编辑注释中的解释:

  1. ! - 声明编译器值一定不会为 null
var val1 = val2! // val2 一定不会为 null
  1. ? - 声明编译器值可以为 null
String? val; // 这里 val 可能为 null
  1. ?. - 只有在不为 null 时访问属性。
object?.prop1; // 只有在 object 不为 null 时才访问 prop1
  1. ?? - 如果值为 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 ??

  1. ! - Saying the compiler value can never be null.
var val1 = val2! // val2 can never be null
  1. ? - Saying the compiler value can be null.
String? val; // Here val can be potentially null
  1. ?. - Access only if not null.
object?.prop1; // Here prop1 is accessed only if object is not null
  1. ?? - 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'),
  ]),

huangapple
  • 本文由 发表于 2023年3月9日 13:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定