简体   繁体   English

Dart的广播流中forEach会泄漏吗?

[英]Will forEach leak in a Dart's Broadcast Stream?

I have a StreamController.broadcast() which I pass its Steam to many of my Flutter Widgets at creation. 我有一个StreamController.broadcast() ,将它的Steam传递给创建时的许多Flutter Widget。

Since at each build I will be recreating those Widgets, I wonder if using Stream.forEach() inside those Widgets won't lead to memory leaks, as forEach() has no mechanism to close the Subscription . 由于在每次build我都将重新创建这些Widget,所以我想知道在这些Widget中使用Stream.forEach()是否不会导致内存泄漏,因为forEach()没有关闭Subscription机制。 I wonder if maybe those Subscription object won't get cleaned-up, or even if the Widgets themselves won't get out of memory. 我不知道这些Subscription对象是否不会被清除,或者Widget本身是否不会耗尽内存。

Yes, it will. 是的,它会的。

The listener is maintained until the stream ends or emits an error. 侦听器一直保持到流结束或发出错误为止。

So if you do Stream.forEach inside build , then any rebuild of the widget will add a listener without removing the previous one. 因此,如果在build内进行Stream.forEach ,则对小部件的任何重新build都会添加一个侦听器,而不会删除前一个侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM