【问题标题】:Dart assert if not null in constructorDart 断言如果在构造函数中不为空
【发布时间】:2020-12-11 13:15:00
【问题描述】:

如何在 Dart 构造函数中断言参数是否为空?

const CardStackCarousel({
    Key key,
    this.itemBuilder,
    this.itemCount,
    int backgroundItemCount,
    this.controller,
    this.offsetAboveBackcards: 10.0,
    this.minScale: 0.8,
    this.verticalAxis: false,
  })  : backgroundItemCount = backgroundItemCount == null ? itemCount - 1 : backgroundItemCount,
        assert(backgroundItemCount < itemCount, 'background item must be less than itemCount'),
        super(key: key);

使用上面的代码,我得到一个错误:

The method '<' was called on null.
Receiver: null

当用户没有指定 backgroundItemCount 属性时。所以我的想法是也许我们应该只断言这个属性不为空。但我不知道该怎么做

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这是因为您没有断言 CardStackCarousel 属性 - 可能也有 backgroundItemCount - ,您正在断言您通过构造函数接收的参数,根据您的逻辑,可以为空。

    当你打电话时:

    backgroundItemCount = backgroundItemCount == null ? itemCount - 1 : backgroundItemCount
    

    您没有为收到的输入分配新值,您可能将其保存到本地属性。你在上面的行中实际上在做的是:

    this.backgroundItemCount = backgroundItemCount == null ? itemCount - 1 : backgroundItemCount
    

    这就是断言失败的原因,因为它不检查this.backgroundItemCount,而是检查通过构造函数接收的属性backgroundItemCount

    我会引用Dart official documentation的一句话:

    警告:初始化器的右侧无权访问 这个。

    ...

    在开发过程中,您可以在初始化列表中使用 assert 来验证输入。

    这解释了为什么您不能检查 this,因为它仍然是“静态”验证您的输入 - 在右侧评估时,它还没有实例。


    如果您仍然需要确保backgroundItemCount &lt; itemCount,您应该只需assert(backgroundItemCount == null || backgroundItemCount &lt; itemCount, 'If background item is supplied, it must be less than itemCount')

    像这样:

    const CardStackCarousel({
        Key key,
        this.itemBuilder,
        this.itemCount,
        int backgroundItemCount,
        this.controller,
        this.offsetAboveBackcards: 10.0,
        this.minScale: 0.8,
        this.verticalAxis: false,
      })  : backgroundItemCount = backgroundItemCount == null ? itemCount - 1 : backgroundItemCount,
            assert(backgroundItemCount == null || backgroundItemCount < itemCount, 'If background item is supplied, it must be less than itemCount'),
            super(key: key);
    

    【讨论】:

    • 那么如何断言(backgroundItemCount
    • 我添加了一条关于如何仍然检查itemCount 是否大于backgroundItemCount 的评论
    • 我没有得到你的答案,我试过 const CardStackCarousel({ Key key, this.itemBuilder, this.itemCount, int backgroundItemCount, this.controller, this.offsetAboveBackcards: 10.0, this.minScale: 0.8, this.verticalAxis: false, }) : backgroundItemCount = backgroundItemCount == null ? itemCount - 1 : backgroundItemCount, assert(backgroundItemCount != null &amp;&amp; backgroundItemCount &lt; itemCount, 'background item must be less than itemCount'), super(key: key); 但是当用户省略 backgroundItemCount 时断言总是失败
    • 你把这个assert(backgroundItemCount != null &amp;&amp; backgroundItemCount &lt; itemCount, 'background item must be less than itemCount')放在哪里?你能把构造函数的最终版本吗?
    • 好吧,我已经更新了你的构造函数,以便在你收到的输入为 != null 时检查它,因为正如我所说,你不能检查你的实例属性,因为你没有还没有实例。
    猜你喜欢
    • 2020-08-01
    • 2023-02-17
    • 2012-10-14
    • 2017-08-18
    • 2018-12-03
    • 1970-01-01
    • 2021-09-20
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多