【问题标题】:add event for listener but not builder in flutter bloc在flutter bloc中为侦听器而不是构建器添加事件
【发布时间】:2021-04-25 06:56:51
【问题描述】:

在我想显示对话框的选项卡上使用颤振块。我通过在点击选项卡时向 bloc 添加一个事件来做到这一点。在 BlocConsumer 中,侦听器按预期收听并显示对话框。但是,由于我向 bloc 添加了一个事件,因此 BlocBuilder 重建了预期的行为(但不需要),因为添加了一个事件。我怎样才能防止这种重建?如果需要,我可以添加代码....

【问题讨论】:

  • 你试过在 buildWhen 上提供条件吗?

标签: flutter flutter-bloc


【解决方案1】:

使用 buildWhen 并检查当前状态是否是您的选项卡状态并返回 false 这将阻止该块构建。

BlocConsumer<BlocA, BlocAState>(
  .......

  buildWhen: (previous, current) {
    if(current is YourTabState) {
        return false;
     } 
    return true;
  },

  ......
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多