blocbuilder vs bloclistener

发布于 2025-02-12 20:15:33 字数 767 浏览 0 评论 0原文

阅读文档时,我有一个非常具体的问题。

阅读 blocbuilder 然后,我继续阅读文档,然后继续阅读。 bloclistener 。一切都很清楚,直到我读到这篇文章:

保证,每个状态>侦听器仅被调用一次 与Blocbuilder中的建造者不同。

据我了解,在blocbuilder中,构建器:每次bloc state更改时都会调用(当然,在build时:时它总是返回为默认值)。在bloclistener中,侦听器:响应state bloc中的更改。

我似乎无法理解引用文本背后的概念。 BlocBuilder中的Builder:是否也会为BLOC的state中的每一个更改都打电话一次吗?我错过的那个“第二个”电话在哪里发生?

I have a very specific question when reading the documentation.

After reading the BlocBuilder documentation, I then went on by reading about the BlocListener. Everything was pretty clear, until I read this:

The listener is guaranteed to only be called once for each state
change unlike the builder in BlocBuilder.

As I understand, in BlocBuilder the builder: gets called every time the bloc state changes (of course in the case in which buildWhen: is omitted and it always returns true as a default value). And in BlocListener the listener: gets called in response to state changes in the bloc.

I can't seem to understand the concept behind the quoted text. Doesn't the builder: in BlocBuilder also get called once for every change in the state of the bloc? Where's that "second" call I'm missing happening?

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

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

发布评论

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

评论(3

够钟 2025-02-19 20:15:33

建造者按照状态变化的方式运行。但是,当框架认为重建所需的框架时,构建器功能也会运行。

侦听器功能不受重建框架的影响。

The builder is run as you say upon state change. But the builder function is also run when the framework deems necessary to rebuild.

The listener function is not affected by the frameworks need to rebuild.

吃素的狼 2025-02-19 20:15:33

每次将新状态由集团发出新状态时,都会将其与上一个相结合,如果它们是不同的,则会触发侦听器函数。

如果在您的情况下,您仅使用加载作为道具,这意味着仅测试两个状态时,仅测试了两个状态。以这种方式,集团消费者认为这两个状态是平等的,并且 再次触发了侦听器功能。

如果要更新同一状态,只需在调用更新状态之前添加一个状态并致电'加载'状态SO bloclistener blocbuilder 会听

Every time when a new state is emitted by the bloc it is compared with the previous one, and if they are DIFFERENT, the listener function is triggered.

If in you situation, you are using Equatable with only LoadedState as props, which means when two states are compared only the LoadedState are tested. In this way BLoC consumer thinks that the two states are equal and do not triggers the listener function again.

if you want to update same state just add one state before calling your updating state like this, if you want to update 'LoadedState' state again call 'LoadingState' state before that and than call 'LoadedState' state so BlocListener and BlocBuilder will listen to it

情徒 2025-02-19 20:15:33

blocbuilder在每个渲染上都调用当前状态,bloclistener仅在状态更改时调用。

The BlocBuilder is called with the current state on every render, the BlocListener is only called when the state changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文