Flutter StreamBuilder 在推送另一个屏幕后继续更新。

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

Flutter StreamBuilder continue to be updated after push another screen

问题

I'm totally lost with Flutter Bloc and streams..
在使用Flutter Bloc和streams时,我完全不知所措。。

After a lot of debugging I figure out the BlocBuilder doesn't really work well with streams, emit happening but the widget doesn't updating probably it's not enough sensitive to small changes (in a list object fields..).
经过大量调试,我发现BlocBuilder与流并不太兼容,虽然触发了emit,但小的更改(例如列表对象字段)可能不足以敏感地更新小部件。

So I have tried StreamBuilder that will listen to the stream of a list, that works fine even though it doesn't really feel comfortable - what really the bloc for if it doesn't update the widget on changes well?
所以我尝试了使用StreamBuilder来监听列表的流,虽然它运行良好,但并不感到舒适——如果Bloc不能在更改时很好地更新小部件,那它的作用是什么?

Now the problem is after I push another page the StreamBuilder continued to be updated with data.. Is there anyway to pause it - seems like a big waist
现在的问题是,当我推送到另一页后,StreamBuilder继续更新数据...有没有办法暂停它——似乎是一种浪费。

英文:

I'm totally lost with Flutter Bloc and streams..
After a lot of debugging I figure out the BlocBuilder doesn't really work well with streams, emit happening but the widget doesn't updating probably it's not enough sensitive to small changes (in a list object fields..). So I have tried StreamBuilder that will listen to the stream of a list, that works fine even though it doesn't really feel comfortable - what really the bloc for if it doesn't update the widget on changes well?

Now the problem is after I push another page the StreamBuilder continued to be updated with data.. Is there anyway to pause it - seems like a big waist

答案1

得分: 1

如果你希望BlocBuilder基于列表更新而改变,那么你需要复制该列表并发出该复制品。

// 在 bloc 函数中...

final updatedList = [...state.myList]; // 使用扩展运算符复制列表

// 更新你的列表
// 发出 updatedList,BlocBuilder 将做出响应

没有理由必须使用 StreamBuilders。使用 Bloc widget,一旦页面离开导航栈,它将不会对任何更改做出反应。如果你推送一个新页面到栈上而不弹出前一个页面,那么任何监听器仍将处于活动状态。

你可以使用 BlocBuilder 和 BlocListener widgets 的 buildWhenlistenWhen 过滤器来检查你在导航中的位置,如果需要保留前一页在导航栈中。

BlocListener<YourBloc, YourState>(
      listenWhen: (previous, current) {
        // 检查当前页面是否为当前页面,然后再对状态更改做出反应
      },
      listener: (context, state) {
         ...
      },
);
英文:

If you want BlocBuilder to change based on a list update then you need to make a copy of the list and emit that.

// in bloc function...

final updatedList = [...state.myList]; // making a copy of the list using spread operator

// update your list
// emit updatedList and BlocBuilder will react

There's no reason to have to use StreamBuilders. Using a Bloc widget, once that page is off of the navigation stack, it won't react to any changes. If you push a new page to the stack without popping off the previous one, then any listeners will still be active.

You can use the buildWhen or listenWhen filters of the BlocBuilder and BlocListener widgets to check where you are in the navigation if you need the previous page to remain in the navigation stack.

BlocListener&lt;YourBloc, YourState&gt;(
      listenWhen: (previous, current) {
        // check if current page is the current page before reacting to state changes
      },
      listener: (context, state) {
         ...
        },
    );

huangapple
  • 本文由 发表于 2023年5月11日 17:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226347.html
匿名

发表评论

匿名网友

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

确定