参数类型; double?'可以将分配给参数类型' num',即使我检查值是否不是null

发布于 2025-02-02 00:30:48 字数 325 浏览 2 评论 0原文

我有此功能:

double discount() {
    return cost_min_discounted != null ? cost_min - cost_min_discounted : 0;
}

cost_min_discounted定义为double?
我得到错误:

参数类型'double?'无法分配给参数类型 'num'

编写这样的代码的最佳方法是什么?我检查了空值,因此代码对我来说似乎是正确的。

I have this function:

double discount() {
    return cost_min_discounted != null ? cost_min - cost_min_discounted : 0;
}

cost_min_discounted is defined as double?
I get the error:

The argument type 'double?' can't be assigned to the parameter type
'num'

What's the best way to write code like that? I check for the null value, so the code seems correct to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2025-02-09 00:30:48

当您已经在开始时检查NULL时,在最后使用

double discount() {
  return cost_min_discounted != null ? cost_min - cost_min_discounted! : 0;
}

While you've already checking null at beginning, use ! at the end.

double discount() {
  return cost_min_discounted != null ? cost_min - cost_min_discounted! : 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文